import java.awt.*;
import java.awt.event.*;
class game0111 extends Frame implements Runnable{
int t;
int px,py;
int m,n;
int z[][]=new int[11][11];
int coin;
int plan;
public static void main(String [] args) {
Frame f=new game0111();
f.setTitle("game0111");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.setVisible(true);
}
game0111(){
setLayout(new FlowLayout());
Button bu1 = new Button("1");
bu1.addActionListener(new beck());
add(bu1);
bu1.setActionCommand("c1");
Button bu2 = new Button("2");
bu2.addActionListener(new beck());
add(bu2);
bu2.setActionCommand("c2");
Button bu3 = new Button("3");
bu3.addActionListener(new beck());
add(bu3);
bu3.setActionCommand("c3");
Button bu4 = new Button("4");
bu4.addActionListener(new beck());
add(bu4);
bu4.setActionCommand("c4");
setVisible( true );
coin=1;
Thread th=new Thread(this);
th.start();
addWindowListener(new stopwin());
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
class beck implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ("c1".equals(e.getActionCommand()))coin=1;
if ("c2".equals(e.getActionCommand()))coin=2;
if ("c3".equals(e.getActionCommand()))coin=3;
if ("c4".equals(e.getActionCommand()))coin=4;
}
}
public void run(){
for (m=1;m<11;m++){
for (n=1;n<11;n++){
z[m][n]=1;
}
}
px=1;
py=1;
t=1;
while(t<100){
if (coin==1)px=px+1;
if (coin==2)px=px-1;
if (coin==3)py=py+1;
if (coin==4)py=py-1;
if (px<1)px=1;
if (px>10)px=10;
if (py<1)py=1;
if (py>10)py=10;
z[px][py]=0;
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
t=t+1;
}
repaint();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(100+50*px,100+50*py,50,50);
for (m=1;m<11;m++){
for (n=1;n<11;n++){
if (z[m][n]==1)g.fillRect(100+50*m,100+50*n,10,10);
}
}
}
}
最終更新:2011年02月03日 21:25