core java当中的MouseTest小程序

声明:该程序是从core java中copy过来的,用于个人学习。

测试鼠标的响应的事件

  1. import javax.swing.*;
  2. import java.util.*;
  3. import java.awt.geom.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. public class MouseTest {
  7. public static void main(String[]args){
  8.     MouseFrame  frame=new MouseFrame();
  9.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.     frame.show();
  11. }
  12. }
  13. class MouseFrame extends JFrame{
  14.     public MouseFrame(){
  15.         setTitle("MouseTest--phenix");
  16.         setSize(this.DEFAULT_WIDTH,this.DEFAULT_HEIGHT);
  17.         MousePanel  panel=new MousePanel();
  18.         Container  contentPane=getContentPane();
  19.         contentPane.add(panel);
  20.     }
  21.     public static final int DEFAULT_WIDTH=300;
  22.     public static final int DEFAULT_HEIGHT=300;
  23. }
  24. class MousePanel extends JPanel{
  25.     private static final int SIDELENGTH=20;
  26.     private ArrayList squares;
  27.     private Rectangle2D  current;
  28.     public MousePanel(){
  29.         squares =new ArrayList();
  30.         current=null;
  31.         addMouseListener(new MouseHandler());
  32.         addMouseMotionListener(new MouseMotionHandler());
  33.     }
  34.     public Rectangle2D  find(Point2D p){
  35.         for(int i=0;i<squares.size();i++){
  36.             Rectangle2D  r=(Rectangle2D)squares.get(i);
  37.             if(r.contains(p))return r;
  38.         }
  39.         return null;
  40.     }
  41.     //添加一个矩形
  42.     public void add(Point2D p){
  43.         double  x=p.getX();
  44.         double  y=p.getY();
  45.         
  46.         current=new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
  47.         squares.add(current);
  48.         repaint();
  49.     }
  50.     //移除矩形
  51.     public void remove(Rectangle2D p){
  52.         if(p==null)return;
  53.         if(p==current)current=null;
  54.         squares.remove(p);
  55.         repaint();
  56.     }
  57.     public void paintComponent(Graphics g){
  58.         super.paintComponent(g);
  59.         Graphics2D g2=(Graphics2D)g;
  60.         for(int i=0;i<squares.size();i++){
  61.             g2.draw((Rectangle2D)squares.get(i));
  62.         }
  63.     }
  64.     private class MouseHandler extends MouseAdapter{
  65.     public void mousePressed(MouseEvent event){
  66.         current=find(event.getPoint());
  67.         if(current==null){
  68.             add(event.getPoint());
  69.         }
  70.     }
  71.     public void mouseClicked(MouseEvent event){
  72.         current=find(event.getPoint());
  73.         if(current!=null&&event.getClickCount()>=2){
  74.             remove(current);
  75.         }
  76.     }
  77.     }
  78.     private class MouseMotionHandler implements MouseMotionListener{
  79.         public void mouseMoved(MouseEvent event){
  80.             if(find(event.getPoint())==null){
  81.                 setCursor(Cursor.getDefaultCursor());
  82.             }
  83.             else
  84.             {
  85.                 setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  86.             }
  87.         }
  88.         public void mouseDragged(MouseEvent event){
  89.             if(current!=null){
  90.                 int x=event.getX();
  91.                 int y=event.getY();
  92.                 current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
  93.                 repaint();
  94.             }
  95.         }
  96.     }
  97. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值