Java learning - Draw drawing board

topic

At the interface between arbitrarily selected four points, denoted as ABC and P.
In the ABC randomly selecting a point, and taking the midpoint of the point P, rendering the midpoint, referred to as the midpoint and the new P.

Knowledge Point

Graphics drawing provides related methods, in accordance with a drawing assembly located after the visible components, a method call getGraphics

Explain the key code

package window;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class DrawListener implements MouseListener{
   Graphics g ;
	
	//设置画布的方法
	public void setGraphics(Graphics g) {
		this.g = g;
	}

	//记录坐标使用的数字
	int startx,starty,endx,endy;
	
	public void mousePressed(MouseEvent e) {
		//记录坐标
		startx = e.getX();
		starty = e.getY();
	}
	int i=0;
	double[][] a=new double[4][2];
    public void mouseReleased(MouseEvent e) {
    	//记录坐标
    	endx = e.getX();
    	endy = e.getY();
    	//画线
    	//g.drawLine(startx, starty, endx, endy);
    	if(i<4)
    	point(startx, starty, endx, endy);
    	else{
    		Random rand = new Random();
    		int number=100;
    		while(number>0){
    			int num=rand.nextInt(3)+1;
    			switch(num){
    			case 1:a[3][0]=(a[0][0]+a[3][0])/2;a[3][1]=(a[0][1]+a[3][1])/2;
    			System.out.println("当前P点的坐标值为"+"("+a[3][0]+","+a[3][1]+")");break;
    			case 2:a[3][0]=(a[1][0]+a[3][0])/2;a[3][1]=(a[1][1]+a[3][1])/2;
    			System.out.println("当前P点的坐标值为"+"("+a[3][0]+","+a[3][1]+")");break;
    			case 3:a[3][0]=(a[2][0]+a[3][0])/2;a[3][1]=(a[2][1]+a[3][1])/2;
    			System.out.println("当前P点的坐标值为"+"("+a[3][0]+","+a[3][1]+")");break;
    			}
    		}
    		
    	}
    }
    public void point(int startx, int starty, int endx, int endy){
    	a[i][0]=endx;
    	a[i][1]=endy;
    	System.out.println("当前坐标为"+"("+endx+","+endy+")");
    	i++;
    }
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

Show results

Here Insert Picture Description

Published 32 original articles · won praise 25 · views 4110

Guess you like

Origin blog.csdn.net/weixin_44143695/article/details/103264767