import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
/**
 * @author binbin
 * 实现功能:
 * click鼠标(MouseHandler),若没有小方格(find()),添加一个(add());
 * 如果ClickCount>2则消除这个小方格(MouseClicked());
 * 可以拖动小方格(MouseMotionHandler);
 * 当鼠标移动到方格内则变成十字形,否则是箭头形。
 */
public class MouseTest {
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				MouseFrame frame = new MouseFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}

}

class MouseFrame extends JFrame
{
	public MouseFrame()
	{
		setTitle("MouseTest");
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		//add component to frame
		MouseComponent component = new MouseComponent();
		add(component);
	}
	public static final int DEFAULT_WIDTH = 300;
	public static final int DEFAULT_HEIGHT = 300;
}


class MouseComponent extends JComponent
{
	public MouseComponent()
	{
		squares = new ArrayList<Rectangle2D>();
		current = null;
		
		addMouseListener(new MouseHandler());
		addMouseMotionListener(new MouseMotionHandler());
	}
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		//draw all squares
		for(Rectangle2D r:squares)
			g2.draw(r);
	}
	public Rectangle2D find(Point2D p){
		for(Rectangle2D r:squares)
		{
			if(r.contains(p)) return r;
		}
		return null;
	}
	public void add(Point2D p){
		double x = p.getX();
		double y = p.getY();
		
		current = new Rectangle2D.Double(x - SIDELENGTH/2,y - SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
		squares.add(current);
		repaint();
	}
	public void remove(Rectangle2D s){
		if(s == null) return;
		if(s == current) current = null;
		squares.remove(s);
		repaint();
	}
	private static final int SIDELENGTH = 10;
	private ArrayList<Rectangle2D> squares;
	private Rectangle2D current;


	private class MouseHandler extends MouseAdapter
	{
		public void mousePressed(MouseEvent event){
			//add a new square if the cursor isn't inside a quare
			current = find(event.getPoint());
			if(current ==null) add(event.getPoint());
		}
		public void mouseClicked(MouseEvent event){
			//remove the current square if double clicked
			current = find(event.getPoint());
			if(current != null && event.getClickCount()>=2) remove(current);
			
		}
	}
	private class MouseMotionHandler implements MouseMotionListener
	{
		public void mouseMoved(MouseEvent event)
		{
			//set the mouse cursor to cross hairs if it is inside
			//a rectangel
			if(find(event.getPoint()) == null)setCursor(Cursor.getDefaultCursor());
			else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		public void mouseDragged(MouseEvent event)
		{
			if(current != null)
			{
				int x = event.getX();
				int y = event.getY();
				//drag the current rectangle to center it at (x,y)
				current.setFrame(x - SIDELENGTH/2,y - SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
				repaint();
				
			}
		}
	}
}
posted on 2011-05-04 09:32  aodixius  阅读(511)  评论(0编辑  收藏  举报