import java.awt.*;
import java.awt.event.*;
class game1207 extends Frame implements Runnable ,MouseMotionListener{
int x,y;
int t;
int coin;
public static void main(String [] args) {
Frame f=new game1207();
f.setTitle("game1207");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.setVisible(true);
}
game1207(){
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(){
t=1;
coin=0;
x=100;
y=100;
while(t<500){
if(coin==1)x=x+10;
if(coin==2)x=x-10;
if(coin==3)y=y+10;
if(coin==4)y=y-10;
if(x>550)x=550;
if(x<150)x=150;
if(y>550)y=550;
if(y<150)y=150;
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
t=t+1;
}
repaint();
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int moux,mouy;
mouy=e.getY() ;
moux=e.getX();
coin=0;
if(moux>x+50)coin=1;
if(moux<x-50)coin=2;
if(mouy>y+50)coin=3;
if(mouy<y-50)coin=4;
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(100,100,500,500);
g.setColor(Color.red);
g.fillRect(x-5,y-5,10,10);
}
}
最終更新:2011年02月10日 16:54