⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawrect2.java

📁 这是两个画矩形的小程序、程序主要通过获取鼠标的坐标
💻 JAVA
字号:
package PaintRect;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class DrawRect2 extends JFrame{

	/**
	 * @param args
	 */
	private Point point1[]=new Point[50];
	private Point point2[]=new Point[50];
	private boolean check;
	private int pointCount=0;

	public DrawRect2(){
		
		super("draw a rect2");
		
		addMouseListener(new MousePressAndReleased());
		addMouseMotionListener(new MouseDragged());

		setSize(500,500);
		setVisible(true);
	
	}
	public static void main(String[] args) {
		
		DrawRect2 application=new DrawRect2();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	}
	
	private class MousePressAndReleased extends MouseAdapter{
		
		public void mousePressed(MouseEvent event){
			
			if(pointCount<point1.length){
				point1[pointCount]=event.getPoint();
				check=false;
			}
		} 
		
		public void mouseReleased(MouseEvent event){

			if (check==true) {
				pointCount=pointCount+1;
			}
		
		}
	
	}
	
	private class MouseDragged extends MouseMotionAdapter{
		
		public void mouseDragged(MouseEvent event){
			
			if(pointCount<point2.length){
				point2[pointCount]=event.getPoint();
			}

			repaint();
			check=true;

		}
	}
	
	public void paint(Graphics g){
		
		super.paint(g);

		for(int i=0; i<point1.length&&point1[i]!=null;i++)
			g.drawRect(Math.min(point1[i].x,point2[i].x), 
					Math.min(point1[i].y,point2[i].y), 
					Math.abs(point2[i].x-point1[i].x),
					Math.abs(point2[i].y-point1[i].y));

	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -