Java mouse events, click the left button to draw a circle, right click to draw a rectangle, double click the left button to clear

Title description

Write a Java program, create a form, and implement interaction. In the form, "click the left mouse button", draw a "green circle" at the cursor; "click the right mouse button" in the form, draw a "red box" at the cursor; "double click the left mouse button" in the form Key" to clear all the drawn "circles" and "squares".

One, the basic idea

The topic is a simple form interaction, the main code is to add a mouse event listener to the panel, and then implement the listener.


Second, the referenced class analysis

1. To realize drawing in the window, the Graphics class needs to be introduced.

2. To realize the mouse double-click event, the mouse double-click event can be realized by judging the number of mouse clicks through the event source e.getClickCount()==2. But in this case, the program will execute the left-click event again before executing the double-click event. In order to avoid this situation, the timer class Timer in java is used here.

Timer method schedule (TimerTask task, Date firstTime, long period) This method is to schedule a task, starting from the event firstTime, and after each scheduling, wait for the period (ms) to continue scheduling.

(The use of the Timer class in the implemented code is borrowed from the content of many bloggers' articles)


Three, code implementation

import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MyFrame extends JFrame {
    
    
	MyPanel panel;

	MyFrame() {
    
    
		panel = new MyPanel();
		this.setBounds(500, 500, 600, 600);
		panel.addMouseListener(new Draw()); // 为面板添加鼠标监听器
		this.add(panel);
		this.setVisible(true);
	}

	public static void main(String[] args) {
    
    
		MyFrame mf = new MyFrame();
	}
}

class MyPanel extends JPanel {
    
    
	int judge = 0; // 判断鼠标点击状态
	int x;
	int y;

	@Override
	public void paint(Graphics g) {
    
    
		if (judge == 1) // 单击左键
		{
    
    
			g.setColor(Color.GREEN);
			g.fillOval(x, y, 50, 50);
		} else if (judge == 2) // 双击左键
		{
    
    
			g.setColor(Color.WHITE);
			super.paint(g);
		} else if (judge == 3) // 单击右键
		{
    
    
			g.setColor(Color.RED);
			g.fillRect(x, y, 50, 50);
		}
	}
}

class Draw extends MouseAdapter {
    
    

	static boolean f = false;
	static int n = 1;

	@Override
	public void mouseClicked(MouseEvent e) {
    
     // 实现监听器

		final MyPanel mp = (MyPanel) e.getSource();

		mp.x = e.getX(); // 获取鼠标点击的位置,为画图确定坐标
		mp.y = e.getY();

		f = false;

		if (e.getButton() == e.BUTTON3) {
    
     // 单击右键
			n = 3;
			mp.judge = n;
			mp.repaint();
			n = 1;
			return;
		}

		else if (e.getButton() == e.BUTTON1) {
    
    

			if (n == 2) {
    
     // 双击左键
				mp.judge = n;
				mp.repaint();
				n = 1;
				f = true;
				return;
			}

			Timer timer = new Timer(); // 使用定时器来判断双击和单击左键

			timer.schedule(new TimerTask() {
    
    

				int num = 0;

				@Override
				public void run() {
    
    

					if (f) {
    
     // 判断双击事件是否已经执行,若已执行则取消定时器任务
						num = 0;
						n = 1;
						this.cancel();
						return;
					}

					if (num == 1) {
    
    
						mp.judge = num; // 定时器再次执行,调用左键单击事件,然后取消定时器任务
						mp.repaint();
						f = true;
						n = 1;
						num = 0;
						this.cancel();
						return;
					}
					n++;
					num++;
				}
			}, new Date(), 150);
		}

	}
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_46027243/article/details/109062343