awt应用

在组件中显示信息

import java.awt.*;
import javax.swing.*;
public class hello {
    public static void main(String[] args)
    {
        EventQueue.invokeLater(()->{
            NotHelloWordsFrame frame =new NotHelloWordsFrame();
            frame.setTitle("NotHelloWorld");//设置title属性,这个属性确定窗体标题栏中的文字
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭窗口操作,
            frame.setVisible(true);//设置visible属性,组件最初是可见的,但类似JFrame的顶层组件除外
        });
    }
}

class NotHelloWordsFrame extends JFrame{
    public NotHelloWordsFrame()
    {
        add(new NotHelloWordsComponent());
        pack();
    }
}
class NotHelloWordsComponent extends JComponent{
    public static final int MESSAGE_X =75;
    public static final int MESSAGE_Y=100;

    private static final int DEFAULT_WIDTH =300;
    private static final int DEFAULT_HEIGHT=200;
    public void paintComponent(Graphics g)
    {
        g.drawString("NOT a ,world program",MESSAGE_X,MESSAGE_X);
    }
    public Dimension getPreferredSize()
    {
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
}

在这里插入图片描述

处理2D图形

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class hello {
    public static void main(String[] args)
    {
        EventQueue.invokeLater(()->{
            DrawFrame frame=new DrawFrame();
            frame.setTitle("Drawrame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}
class DrawFrame extends JFrame{
    public DrawFrame()
    {
        add(new DrawComponent());//将一个给定的组件添加到该窗体的内容窗格中,并返回这个动作
        pack();//调整窗口大小
    }
}

class DrawComponent extends JComponent{
    private static int DEFAULT_WIDTH=400;
    private static 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);

        Ellipse2D ellipse =new Ellipse2D.Double();
        ellipse.setFrame(rect);
        g2.draw(ellipse);

        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);
    }
    public Dimension getPreferredSize()//默认优先尺寸
    {
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }

}

在这里插入图片描述

使用字体

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class hello {
    public static void main(String[] args)
    {
        EventQueue.invokeLater(()->{
            FontFrame frame =new FontFrame();
            frame.setTitle("FontTile");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class FontFrame extends JFrame
{
    public FontFrame()
    {
        add(new FrontComponent());
        pack();
    }
}

class FrontComponent extends JComponent{
    private static final int DEFAULT_WIDTH=300;
    private static final int DEFAULT_HEIGHT=200;
    public void paintComponent(Graphics g)
    {
        Graphics2D g2=(Graphics2D) g;
        String message="Hello World";
        Font f= new Font("Serif",Font.BOLD,36);//逻辑字体名  字体风格  字体号
        g2.setFont(f);

        FontRenderContext context =g2.getFontRenderContext();//获得这个图形上下文中指定字体特征的字体绘制上下文
        Rectangle2D bounds=f.getStringBounds(message,context);
        //返回包围这个字符串的矩形。矩形的起点为基线。 矩形顶端的y坐标等于上坡度的负值。矩形的高度等于上坡度下坡度和行距之和

        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);
    }
    public Dimension getPreferredSize()
    {
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值