Java graphical interface --- drawing

Table of contents

1. Introduction to drawing

2. How to draw

3. Case


1. Introduction to drawing

Many programs, such as various games, need to draw various graphics in the window. In addition, even when developing JavaEE projects, sometimes it is necessary to dynamically generate various graphics and charts to the client, such as graphic verification codes, statistics, etc. Figures, etc., which all need to use the drawing function of AWT.

The principle of drawing: We have learned many components before, such as Button, Frame, etc. Different components display different graphics. In fact, the graphics displayed by these components are essentially completed by using AWT drawing.
In AWT, it is the Graphics object that really provides the drawing function, so what is the relationship between the Component component and the Graphics object, so that Components can draw their own graphics? In the Components class, the following three methods are provided to complete the drawing and refreshing of component graphics:
paint(Graphics g) draw the appearance of the component
update(Graphics g) internally call the paint() method to refresh the appearance of the component
repaint() call the update method , to refresh the appearance of the component

2. How to draw

 Drawing step
1: Customize the class, inherit the Canvas class, and rewrite the Paint(Graphics g) method to complete the drawing;
2: Inside the paint method, call the setColor(), setFont() and other methods of the Graphics object before actually starting the drawing. Attributes such as pen color and font
3: Call the drawxxx() method of the Graphics brush to start drawing.

 Commonly used methods:

setColor(Color c)    设置颜色
setFont(Font font)   设置字体
drawLine()           绘制直线
drawRect()           绘制矩形
drawRoundRect()      绘制圆角矩形
drawOval()           绘制椭圆形
drawPolygon()        绘制多边形
drawArc()            绘制圆弧
drawPolyine()        绘制折线
fillRect()           填充矩形区域
fillRoundRect()      填充圆角矩形区域
fillOval()           填充椭圆区域
fillPolygon()        填充多边形区域
fillArc()            填充圆弧对应的扇形区域
drawImage()          绘制位图

3. Case

Requirements: Create an interface as shown in the figure, and click the corresponding button to draw the graph shown.

class Solution {
    //制造组件
    Frame s=new Frame("绘图");
    Button b1=new Button("绘制矩形");
    Button b2=new Button("绘制椭圆");
    private int f=0;
    private class canv extends Canvas{
        @Override
        public void paint(Graphics g) {
            if(f==1){
                //矩形
                g.setColor(Color.red);//设置画笔的颜色
                g.drawRect(50,50,200,200);
            }
            else if(f==2){
                //椭圆
                 g.setColor(Color.BLUE);
                 g.drawOval(50,50,200,100);
            }
        }
    }
    canv can=new canv();

    public void init(){
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f=1;
                can.repaint();
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f=2;
                can.repaint();
            }
        });
        Box b=Box.createHorizontalBox();
        b.add(b1);
        b.add(b2);
        can.setPreferredSize(new Dimension(400,400));

        s.add(b,BorderLayout.SOUTH);
        s.add(can);
        s.pack();
        s.setVisible(true);
    }
}



public class Test {
   public static void main(String[] args){
       new Solution().init();
   }
}

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128631965