Java技术核心卷-swing

/**
 *  文本使用特殊字体
 * @Author mufeng
 * @Date 2019-5-30 15:33
 */
public class FontTest {
    public static void main(String[] args) {
        //事件分派线程
        EventQueue.invokeLater(()->{
            JFrame frame=new FontFrame();
            //用于改变标题栏的文字
            frame.setTitle("FontTest");
            //相应动作
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //可视化
            frame.setVisible(true);
        });

    }
}
class FontFrame extends JFrame{
    public FontFrame(){
       add(new FontComponent());
       pack();
    }
}
class FontComponent extends JComponent{
    private static final int DEFAULT_WIDTH =400;
    private static final int DEFAULT_HEIGHT =400;
    private Image image;
    public FontComponent(){
        URL resource = FontComponent.class.getResource("/loading-icon.gif");
        image=new ImageIcon("mufeng-test/src/test/resources/rxcf.jpg").getImage();
    }
    //重写Window类
    public Dimension getPreferredSize(){
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
    //重写JComponent
    public void paintComponent(Graphics g) {
        Graphics2D g2=(Graphics2D)g;
        g2.drawImage(image,10,10,null);
        String message="Hello, World!";
        //设置字体 SansSerif Serif Monospaced Dialog DialogInput
        Font f=new Font("Lucida Bright",Font.BOLD,36);
        g2.setFont(f);
        //字体属性的描述对象
        FontRenderContext context=g2.getFontRenderContext();
        Rectangle2D bounds=f.getStringBounds(message,context);
        //设置文本左上角的坐标
        double x=(getWidth()-bounds.getWidth())/2;
        double y=(getHeight()-bounds.getHeight())/2;
        double ascent=-bounds.getY();
        double baseY=y+ascent;
        g2.drawString(message,(int)x,(int)baseY);
        g2.setPaint(Color.LIGHT_GRAY);
        //画下划线
        g2.draw(new Line2D.Double(x,baseY,x+bounds.getWidth(),baseY));
        //画矩形
        Rectangle2D rect=new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());
        g2.draw(rect);
    }
}
/**
 * 画几何图形
 * @Author mufeng
 * @Date 2019-5-30 14:28
 */
public class DrawTest {
    public static void main(String[] args) {
        //事件分派线程
        EventQueue.invokeLater(()->{
            JFrame frame=new DrawFrame();
            //用于改变标题栏的文字
            frame.setTitle("DrawTest");
            //相应动作
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //可视化
            frame.setVisible(true);
        });

    }

}
class DrawFrame extends JFrame{
    public DrawFrame() {
        //增加组件
        DrawComponent drawComponent = new DrawComponent();
        drawComponent.setBackground(Color.RED);
       // drawComponent.setForeground(Color.BLUE);
        add(drawComponent);
        pack();
    }
}
class DrawComponent extends JComponent{
    private static final int DEFAULT_WIDTH =400;
    private static final int DEFAULT_HEIGHT =400;
    public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D) g;
        double leftX=100;
        double topY=100;
        double width=200;
        double height=150;
        //画矩形
        Rectangle2D rect=new Rectangle2D.Double(leftX,topY,width,height);
        g2.draw(rect);
        //设置画笔颜色
//        g2.setColor(Color.RED);
        //当前颜色填充图形
//        g2.fill(rect);
        //画椭圆 以矩形为基,画椭圆
       // Ellipse2D ellipse2D=new Ellipse2D.Double(leftX,topY,width,height);
        Ellipse2D ellipse2D=new Ellipse2D.Double();
        ellipse2D.setFrame(rect);
        g2.draw(ellipse2D);
        //画线
        g2.draw(new Line2D.Double(leftX,topY,leftX+width,topY+height));
        //获得闭合矩形的中心
        double centerX=rect.getCenterX();
        double centerY=rect.getCenterY();
        double radius=150;
        //画圆
        Ellipse2D circle=new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX,centerY,centerX+radius,centerY+radius);
        g2.draw(circle);
    }
    //重写Window类
    public Dimension getPreferredSize(){
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值