鼠标监听事件,模拟画图工具

 1 public class TestMouseListener {
 2     public static void main(String[] args) {
 3 new MyFrame01("鼠标监听事件");
 4     }
 5 }
 6 class MyFrame01 extends Frame{
 7     ArrayList points;
 8     public  MyFrame01(String title){
 9         super(title);
10         points = new ArrayList<>();
11         setBounds(200,200,400,400);
12         setVisible(true);
13         this.addMouseListener(new MyMouseLstener());
14     }
15 
16     @Override
17     public void paint(Graphics g) {
18         super.paint(g);
19         Iterator iterator=points.iterator();
20         while (iterator.hasNext()){
21           Point  point= (Point) iterator.next();
22           g.setColor(Color.red);
23         g.fillOval(point.x,point.y,100,100);
24         }
25     }
26 
27 
28     class MyMouseLstener extends MouseAdapter{
29 
30         @Override
31         public void mouseClicked(MouseEvent e) {
//e代表着鼠标,这里的e.getSourse()意思是返回添加监听器的对象为Object类型,需要转换为Frame类
32 MyFrame01 guo =(MyFrame01) e.getSource(); 33 Point point=new Point(e.getX(),e.getY()); 34 points.add(point); 35 guo.repaint(); 36 } 37 } 38 }

 

posted on 2022-08-05 15:37  大风吹过12138  阅读(29)  评论(0)    收藏  举报

导航