码迷,mamicode.com
首页 > 其他好文 > 详细

练习.鼠标控制小球

时间:2015-01-03 13:02:32      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.java7mousecontrolmyball.main;
 2 import java.awt.Frame;
 3 import java.awt.Graphics;
 4 import java.awt.Panel;
 5 import java.awt.event.MouseEvent;
 6 import java.awt.event.MouseMotionListener;
 7 
 8 public class MouseControlMyBall {
 9     public static void main(String[] args) {
10         Frame w = new Frame();
11         w.setSize(300, 400);
12         
13         MyPanel mp = new MyPanel();
14         w.add(mp);
15         
16         w.addMouseMotionListener(mp);
17         mp.addMouseMotionListener(mp);
18         
19         w.show();
20     }
21 }
22 
23 class MyPanel extends Panel implements Runnable , MouseMotionListener {
24     int x = 30;
25     int y = 30;
26     public void paint(Graphics g){
27         g.fillOval(x, y, 30, 30);
28     }
29     @Override
30     public void mouseDragged(MouseEvent arg0) {
31         x = arg0.getX();
32         y = arg0.getY();
33 //        System.out.println(x);
34 //        System.out.println(y);
35         repaint();
36     }
37     @Override
38     public void mouseMoved(MouseEvent arg0) {
39 //        System.out.println(arg0);
40     }
41     @Override
42     public void run() {
43         // TODO Auto-generated method stub
44         
45     }
46 }

 

练习.鼠标控制小球

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4199374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!