Method of Redrawing Graphical Interface in Java

**

Method of Redrawing Graphical Interface in Java

**
  In the process of writing a graphical interface program, we will find that when we stretch, shrink (or maximize or minimize) the form, the graphics previously drawn on the form will disappear.

Why is this?

1) It turns out that the form including other components are drawn by the computer. Once we make the form change, everything before it has to be redrawn. However, there are established redrawing methods for components, etc., but there is no redrawing method for the graphics we draw.

2) It can also be explained like this: the data we draw graphics are stored in memory, and the entire form is drawn by calling the bottom drawing method of the system. We have defined the size of the form when creating the form. If we change the size of the form again, the original form will not meet the display requirements. At this time, all the components on the form will be redrawn again. The paint method is called, which is defined in both JFrame and JPanel.

  For such a situation, we only need to rewrite the paint method . However, the most important point here is that the class must inherit the JFrame class or the JPanel class, otherwise paint() cannot be overridden! ! !

  At the same time, we need to pay attention to one more point: the way the data is stored and obtained, because the addActionListener() method will only be called onceduring the running of the program, and the event handling method will be launched multiple times. So how to store the data and then use it in the paint method is what we need to consider.
  For example, we can first create a Shape class to record graphic information, namely coordinates, type, etc.
The code of the Shape class created is as follows:

package com.yf1014;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 图形类
 * @author yf
 *
 */

//Shape的主要功能:是记录图形信息,即坐标,类型等,然后根据所记录的图形信息来还原图像的。
public class Shape {
    
    
	//属性
	public int x1,y1,x2,y2;
	public String name;
	
	//方法分为构造方法和普通方法
	//构造方法  格式:public 类名(参数类型 参数名,,){}
	//普通方法  格式:public void 类名(参数类型 参数名,,){}
	//每个类都有一个默认的无参构造方法,当自己定义构造方法,默认的构造方法就会被替代
	//作用:1.创建对象    2.初始化属性
	//this:表示本类对象
	//此时该构造方法的作用是:
	//当创建对象时,将构造方法内传进来的参数赋给属性,这样我每创建一个新的对象时,该对象就拥有这些属性了。
	public Shape(int x1,int y1,int x2,int y2,String name){
    
    
		this.x1 = x1;//当参数名和属性名相同时,使用this来使得参数赋值给属性
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.name = name;
	}
	//根据保存的数据还原图形对象
	public void drawShape(Graphics g){
    
    
		if("直线".equals(name)){
    
    
			g.drawLine(x1, y1, x2, y2);
		}
		else if("矩形".equals(name)) {
    
    
			g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
		}
		else if("小熊".equals(name)) {
    
    
			//头
			Color headColor = new Color(224, 178, 142);
			g.setColor(headColor);
			g.fillRect(x1, y1, 110, 110);
			g.fillArc(x1, y1, 20, 20, 1, 2);
			
			//眼睛
			g.setColor(Color.WHITE);
			g.fillOval(x1+20, y1+32, 18, 24);
			g.setColor(Color.WHITE);
			g.fillOval(x1+75, y1+32, 18, 24);
			
			//眼黑
			g.setColor(Color.BLACK);
			g.fillOval(x1+23, y1+36, 10, 14);
			g.setColor(Color.BLACK);
			g.fillOval(x1+78, y1+36, 10, 14);
			
			//嘴巴
			Color mouse = new Color(248, 246, 229);
			g.setColor(mouse);
			g.fillOval(x1+37, y1+57, 40, 30);
			
			//鼻子
			g.setColor(Color.BLACK);
			g.fillOval(x1+53, y1+50, 9, 9);
			
			//左耳
			g.setColor(Color.RED);
			g.fillOval(x1+3, y1+3, 22, 22);
			
			//右耳
			g.setColor(Color.RED);
			g.fillOval(x1+84, y1+3, 22, 22);  
			
			//Shape shape = new Shape(x1, y1, x2, y2, name);
		}		
	}
}

  Then create a MyFrame class , the main function of this class is to override the paint method . First of all, in this class, use the Shape class to create an array shapeArray to store objects (used to store graphics information, namely coordinates, types, etc.).

The code of the MyFrame class is as follows:

package com.yf1014;

import java.awt.Graphics;

import javax.swing.JFrame;

//MyFrame的主要功能是:是将保存在数组中的图形对象信息进行重绘,即重写paint方法的,因为需要在画布上操作,所以需要继承JFrame类。
public class MyFrame extends JFrame {
    
    
	
	// 定义Shape数组,保存对象(用于保存图形信息,即坐标,类型等)
	private Shape[] shapeArray;
	
	//利用普通方法,将传进来的数组类型shapeAraay赋给属性值,也可以采用的构造方法来进行该操作,换言之,这两种方法都可以来实现传参数给属性值
	public void setShapeArray(Shape[] shapeArray){
    
    
		this.shapeArray = shapeArray;
	}
	
	//重写paint方法
	public void paint(Graphics g) {
    
    
		super.paint(g);//先继承父类的paint方法,然后再利用下述方法重写paint方法

		//遍历setShapeArray数组,取出图形对象
		for(int i=0;i<shapeArray.length;i++){
    
    
			Shape shape = shapeArray[i];
			if(shape != null){
    
    
				shape.drawShape(g);    
			}
		}
	}
}

Next, we will create a DrawMouse listener class to implement the event monitoring function.

code show as below:

package com.yf1014;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

//事件处理类
public class DrawMouse implements MouseListener, ActionListener {
    
    
	// 定义Graphics变量gr,用来保存传递过来的文本框对象
	private Graphics gr;
	private int x1, y1, x2, y2;
	private String name;
	//定义Shape数组,保存对象(用于保存图形信息,即坐标,类型等)
	Shape[] shapeArray = new Shape[100];
	//操作数组的下标
	int index = 0;
	
	//构造方法,将传进来的参数g赋给属性gr
	public DrawMouse(Graphics g){
    
    
		gr = g;
	}

	// 定义set方法,初始化gr变量(即为普通方法,这个时候的功能是和上述的构造方法的功能是相同的,然而当我还需要传入另一个参数时,则需要利用普通方法来实现)
	//在本文中,构造方法DrawMouse和普通方法setGr可以任选其一即可。
	public void setGr(Graphics g) {
    
    
		gr = g;
	}
	
	//接下这些东西就是监听器MouseListener和ActionListener的抽象方法的重写。
	public void actionPerformed(ActionEvent e) {
    
    
		//获取按钮上的字符串
		name = e.getActionCommand();
		
		System.out.println("name = "+name);
	}
	public void mouseClicked(MouseEvent e) {
    
    
		System.out.println("点击");
		
		if(e.getClickCount() == 2){
    
    
			
		}
	}

	public void mousePressed(MouseEvent e) {
    
    
		System.out.println("按下");
		x1 = e.getX();
		y1 = e.getY();
	}

	public void mouseReleased(MouseEvent e) {
    
    
		System.out.println("松开");
		x2 = e.getX();
		y2 = e.getY();

		// 设置画笔颜色
		// gr.setColor(Color.BLUE);
		
		if("直线".equals(name)){
    
    
			// 画图
			gr.drawLine(x1, y1, x2, y2);
		
			//创建Shape对象,保存该图形的数据
			Shape shape = new Shape(x1, y1, x2, y2, name);
			//保存shapeArray数组
			shapeArray[index++] = shape;
		}
		else if("矩形".equals(name)){
    
    
			// 矩形
			gr.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
			//创建Shape对象,保存该图形的数据
			Shape shape = new Shape(x1, y1, x2, y2, name);
			//保存shapeArray数组
			shapeArray[index++] = shape;
		}
		else if("小熊".equals(name)) {
    
    
			//头
			Color headColor = new Color(224, 178, 142);
			gr.setColor(headColor);
			gr.fillRect(x1, y1, 110, 110);
			gr.fillArc(x1, y1, 20, 20, 1, 2);
			
			//眼睛
			gr.setColor(Color.WHITE);
			gr.fillOval(x1+20, y1+32, 18, 24);
			gr.setColor(Color.WHITE);
			gr.fillOval(x1+75, y1+32, 18, 24);
			
			//眼黑
			gr.setColor(Color.BLACK);
			gr.fillOval(x1+23, y1+36, 10, 14);
			gr.setColor(Color.BLACK);
			gr.fillOval(x1+78, y1+36, 10, 14);
			
			//嘴巴
			Color mouse = new Color(248, 246, 229);
			gr.setColor(mouse);
			gr.fillOval(x1+37, y1+57, 40, 30);
			
			//鼻子
			gr.setColor(Color.BLACK);
			gr.fillOval(x1+53, y1+50, 9, 9);
			
			//左耳
			gr.setColor(Color.RED);
			gr.fillOval(x1+3, y1+3, 22, 22);
			
			//右耳
			gr.setColor(Color.RED);
			gr.fillOval(x1+84, y1+3, 22, 22);
			
			Shape shape = new Shape(x1, y1, x2, y2, name);
			//保存shapeArray数组
			shapeArray[index++] = shape;
		}
	}

	public void mouseEntered(MouseEvent e) {
    
    
		System.out.println("进入");
	}

	public void mouseExited(MouseEvent e) {
    
    
		System.out.println("退出");
	}
	
}

Main class ----- DrawFrame class

code show as below:

package com.yf1014;

import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

public class DrawFrame {
    
    
	//主程序(程序入口)
	public static void main(String[] args){
    
    
		DrawFrame df = new DrawFrame();
		df.showUI();
	}
	//显示画图工具的界面
	public void showUI(){
    
    
		MyFrame jf = new MyFrame();
		jf.setTitle("画图工具");
		jf.setSize(800, 700);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);
		//创建流式布局
		jf.setLayout(new FlowLayout());
		//添加各种类型的按钮
		JButton jbu = new JButton("直线");
		jf.add(jbu);
		JButton jbu2 = new JButton("矩形");
		jf.add(jbu2);
		JButton jbu3 = new JButton("小熊");
		jf.add(jbu3);
		//设置可见
		jf.setVisible(true);
		
		//画笔:图形画在哪个组件上,画笔就从该组件上获取
		//从窗体上获取画笔对象,一定要在窗体显示可见之后
		Graphics g = jf.getGraphics();
		
		DrawMouse mouse = new DrawMouse(g);
		//给窗体添加鼠标监听器方法
		jf.addMouseListener(mouse);
		
		//给按钮添加动作监听器
		jbu.addActionListener(mouse);
		jbu2.addActionListener(mouse);
		jbu3.addActionListener(mouse);
		
		//把shapeArray数组传递到MyJFrame中
		jf.setShapeArray(mouse.shapeArray);
	}
}

Output result:

Insert picture description here
Display the result in full screen of the form:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39350172/article/details/109216844
Recommended