import java.awt.*;
import java.awt.event.*;
class game0124 extends Frame implements Runnable ,MouseMotionListener{
int px,py;
int mode;
public static void main(String [] args) {
Frame f=new game0124();
f.setTitle("game0124");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.setVisible(true);
}
game0124(){
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;
mode=1;
px=1;
py=1;
t=0;
while(t<500){
if(mode==1)px=px+1;
if(mode==4)px=px-1;
if(mode==2)py=py+1;
if(mode==3)py=py-1;
repaint();
try{
Thread.sleep(300);
}catch(InterruptedException e){}
t=t+1;
}
repaint();
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int x,y;
int h;
y=e.getY() ;
x=e.getX();
mode=5;
h=0;
if(x<175)h=h+1;
if(y>500)h=h+1;
if(h==2)mode=1;
h=0;
if(x>175)h=h+1;
if(x<350)h=h+1;
if(y>500)h=h+1;
if(h==3)mode=2;
h=0;
if(x>350)h=h+1;
if(x<525)h=h+1;
if(y>500)h=h+1;
if(h==3)mode=3;
h=0;
if(x>525)h=h+1;
if(y>500)h=h+1;
if(h==2)mode=4;
}
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(0,500,175,200);
g.setColor(Color.green);
g.fillRect(175,500,175,200);
g.setColor(Color.red);
g.fillRect(350,500,175,200);
g.setColor(Color.green);
g.fillRect(525,500,175,200);
g.setColor(Color.black);
g.fillRect(100+50*px,100+50*py,50,50);
g.drawString("左",100,600);
g.drawString("下",275,600);
g.drawString("上",450,600);
g.drawString("右",625,600);
}
}
最終更新:2011年02月07日 06:28