shannonredeemed

跟着王洋老师学编程 - 1.9 鼠标控制小球

定义任务

让鼠标能够拖动屏幕中的小球。

我的思路

1. 创建Frame窗口对象

2. 创建画布类实现MouseMotionListener接口,实现接口中的mouseDragged(MouseEvent e)方法

3. 将画布添加至窗口,并为窗口注册鼠标移动事件即MouseMotionListener

 代码一

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 
 4 public class MouseBall extends Frame{
 5     public static void main(String[] args){
 6         Frame w = new Frame();
 7         w.setSize(700,500);
 8 
 9         MouseBallPanel mbp = new MouseBallPanel();
10         w.add(mbp);
11 
12         w.addMouseMotionListner(mbp);
13         mbp.addMouseMotionListner(mbp);
14         
15         w.setVisible(true);
16         
17     }
18 }
19 
20 class MouseBallPanel extends Panel implements MouseMotionListener{
21         
22     public void paint(Graphics g){
23         g.fillOval(300,200,10,10);
24     }
25 
26     public void mouseDragged(MouseEvent e){
27 
28     }
29     public void mouseMoved(MouseEvent e){
30         
31     }
32 }

编译并运行,一直报找不到变量w的错。修改引入的包、修改继承的接口,均没有解决问题,直到发现注册接口处的拼写错误……

修改后,继续补充接口实现部分

代码二

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 
 4 public class MouseBall extends Frame{
 5     public static void main(String[] args){
 6         Frame w = new Frame();
 7         w.setSize(700,500);
 8 
 9         MouseBallPanel mbp = new MouseBallPanel();
10         w.add(mbp);
11 
12         w.addMouseMotionListener(mbp);
13         mbp.addMouseMotionListener(mbp);
14         
15         w.setVisible(true);
16         
17     }
18 }
19 
20 class MouseBallPanel extends Panel implements MouseMotionListener{
21     
22     Point p = new Point(300,200);
23     
24     public void paint(Graphics g){
25         g.fillOval(p,10,10);
26     }
27 
28     public void mouseDragged(MouseEvent e){
29         p=getPoint();
30         repaint();
31     }
32     public void mouseMoved(MouseEvent e){
33         
34     }
35 }

编译并运行,报错如下:

 修复MouseEvent方法调用缺陷、并进行(int)强制类型转换,更新代码如下。

代码三

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 
 4 public class MouseBall extends Frame{
 5     public static void main(String[] args){
 6         Frame w = new Frame();
 7         w.setSize(700,500);
 8 
 9         MouseBallPanel mbp = new MouseBallPanel();
10         w.add(mbp);
11 
12         w.addMouseMotionListener(mbp);
13         mbp.addMouseMotionListener(mbp);
14         
15         w.setVisible(true);
16         
17     }
18 }
19 
20 class MouseBallPanel extends Panel implements MouseMotionListener{
21     
22     Point p = new Point(300,200);
23     
24     public void paint(Graphics g){
25         g.fillOval((int)(p.getX()),(int)(p.getY()),10,10);
26     }
27 
28     public void mouseDragged(MouseEvent e){
29         p=e. getPoint();
30         repaint();
31     }
32     public void mouseMoved(MouseEvent e){
33         
34     }
35 }

编译并运行,成功咯!

 

老师的思路

基本一致,但导入的event包是具体的,且没有使用强制类型转换。

 1 import java.awt.*;
 2 import java.awt.event.MouseEvent;
 3 import java.awt.event.MouseMotionListener;
 4 
 5 public class MouseBallT{
 6     public static void main(String[] args){
 7         Frame w = new Frame();
 8         w.setSize(500,400);
 9 
10         MouseBallPanelT mbpt = new MouseBallPanelT();
11         w.add(mbpt);
12 
13         w.addMouseMotionListener(mbpt);
14         mbpt.addMouseMotionListener(mbpt);
15 
16         w.setVisible(true);        
17     }
18 }
19 
20 class MouseBallPanelT extends Panel implements MouseMotionListener{
21     int x = 30;
22     int y = 40;
23 
24     public void paint(Graphics g){
25         g.fillOval(x,y,15,15);
26     }
27 
28     public void mouseDragged(MouseEvent e){
29         x = e.getX();
30         y = e.getY();
31         repaint(); 
32     }
33 
34     public void mouseMoved(MouseEvent e){
35 
36     }
37 }

 

posted on 2024-03-06 15:19  Shannon_Zhang  阅读(8)  评论(0编辑  收藏  举报