import java.awt.*;
import java.awt.event.*;
class game extends Frame implements Runnable ,MouseMotionListener{
int x,y;
public static void main(String [] args) {
Frame f=new game();
f.setTitle("game");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.setVisible(true);
}
game(){
x=100;
y=100;
addMouseMotionListener(this);
Thread th=new Thread(this);
th.start();
addWindowListener(new stopwin());
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
public void run(){
int t;
t=0;
while(t<100){
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
t=t+1;
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int mx,my;
my=e.getY() ;
mx=e.getX();
if(x>mx)x=x-10;
if(y>my)y=y-10;
if(x<mx)x=x+10;
if(y<my)y=y+10;
}
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(x,y,100,100);
}
}
最終更新:2011年07月12日 14:52