import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
public class MouseTest{
public static void main(String args[]){
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with a panel that displays faling raindign raindrops
*/
class MouseFrame extends JFrame{
public MouseFrame(){
setTitle("MouseTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
MousePanel panel = new MousePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static int DEFAULT_WIDTH = 300;
public static int DEFAULT_HEIGHT =200;
}
/**
A panel that displays falling rain drops
*/
class MousePanel extends JPanel{
public MousePanel(){
squares = new ArrayList();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.red);
// draw all squares
for(int i =0;i < squares.size(); i++)
g2.draw((Rectangle2D)squares.get(i));
}
public Rectangle2D find(Point2D p){
for(int i=0;i<squares.size();i++){
Rectangle2D r=(Rectangle2D)squares.get(i);
if(r.contains(p))
return r;
}
return null;
}
/**
Adds square to the collection.
@param p the center of the square
*/
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();
}
/**
Removes a square from the collection.
@param s the center of the remove
*/
public void remove(Rectangle2D s){
if(s == null)
return;
if(s == current)
current = null;
squares.remove(s);
repaint();
}
private static int SIDELENGTH = 10;
private ArrayList squares;
private Rectangle2D current;
// the square containing the mouse cursor
public class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent event){
//add a new square if the cursor isn't inside a square
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);
}
}
public class MouseMotionHandler implements MouseMotionListener{
public void mouseMoved(MouseEvent event){
// set the mouse cursor to cross hairs if it is inside a rectangle
if(find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
// 这些任意选一个,以显示鼠标不同的效果。
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); // 精确定位形式的鼠标
//setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); // 移动的鼠标
//setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); // 默认的鼠标形式,如桌面上的
//setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // 等待形式的鼠标
//setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); //文本输入
//setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); //正中定位大小的形式
//setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); //西北方向的形式
//setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); //向东的的形式
//setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
//setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
//setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
//setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
//setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_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();
}
}
}
}