One of the Mouse Event

 
 
     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();
  }
         }
  }
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码We now want to always redraw all the points that have ever been drawn in the panel, not just the last point. To do this, we must save the coordinates of all these points so that we can redraw them all one by one in the paintComponent method every time this method is called. To save the coordinates of the various mouse positions we click, replace the x and y instance variables of the MyPanel class with a single private instance variable called points of type ArrayList<Point>. The Point class is provided to you by Swing. In the constructor of MyPanel, initialize the points instance variable with a new arraylist object of the same type. In the mouseClicked method of the mouse listener, use the getPoint method of the mouse event object to get a Point object representing the position of the mouse click (that Point object internally stores both the x and y coordinates of the mouse click event). Then add this Point object to the arraylist using the arraylist’s add method. Then, in the paintComponent method, add a loop to draw in the panel all the points of the arraylist. You can get the number of elements in the arraylist by using the size method of the arraylist; you can access a specific element of the arraylist at index i by using the get(i) method of the arraylist (element indexes start at zero in an arraylist). The Point class has getX and getY methods to get the coordinates of the point (these two methods return values of type double so you need to cast the returned values into the int type before you can use them to draw a point).
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值