import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
class
pro extends Frame implements Runnable{
Frame f;
Thread th;
int t,px,py;
int dx,dy;
int m,n;
int x[][]=
new int[11][11];
public static void main(String[] args){
Frame f=new pro();
f.setTitle("pro");
f.setSize(700,700);
f.setBackground(Color.yellow);
f.show();
}
class beck implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ("c1".equals(e.getActionCommand()))dx=1;
if ("c2".equals(e.getActionCommand()))dx=-1;
if ("c3".equals(e.getActionCommand()))dy=1;
if ("c4".equals(e.getActionCommand()))dy=-1;
if ("c5".equals(e.getActionCommand()))dx=0;
if ("c5".equals(e.getActionCommand()))dy=0;
}
}
pro() {
setLayout(new FlowLayout());
Button bu1 = new Button("right");
bu1.addActionListener(new beck());
add(bu1);
bu1.setActionCommand("c1");
Button bu2 = new Button("left");
bu2.addActionListener(new beck());
add(bu2);
bu2.setActionCommand("c2");
Button bu3 = new Button("up");
bu3.addActionListener(new beck());
add(bu3);
bu3.setActionCommand("c3");
Button bu4 = new Button("down");
bu4.addActionListener(new beck());
add(bu4);
bu4.setActionCommand("c4");
Button bu5 = new Button("
stop");
bu5.addActionListener(new beck());
add(bu5);
bu5.setActionCommand("c5");
addWindowListener(new stopwin());
t=0;
px=1;
py=1;
dx=1;
dy=1;
for (m=1;m<11;m++){
for (n=1;n<11;n++){
x[m][n]=0;
}
}
th=new Thread(this);
th.start();
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
public void paint( Graphics g ) {
g.setColor(Color.yellow);
g.fillRect(0,0,700,700);
g.setColor(Color.red);
g.fillOval(50*px+
100,600-50*py,10,10);
for (m=1;m<11;m++){
for (n=1;n<11;n++){
if (x[m][n]==0)g.fillOval(50*m+100,600-50*n,5,5);
}
}
}
public void update(Graphics g) {
paint(g);
}
public void run() {
double r;
while (t<1000){
px=px+dx;
py=py+dy;
if (px>10)px=10;
if (px<1)px=1;
if (py>10)py=10;
if (py<1)py=1;
x[px][py]=1;
repaint();
try{th.sleep(500);} catch(InterruptedException e) { }
t=t+1;
}
}
}
最終更新:2010年10月11日 00:09