首页 > 代码库 > 练习.鼠标控制小球

练习.鼠标控制小球

 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     @Override30     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     @Override38     public void mouseMoved(MouseEvent arg0) {39 //        System.out.println(arg0);40     }41     @Override42     public void run() {43         // TODO Auto-generated method stub44         45     }46 }

 

练习.鼠标控制小球