アットウィキロゴ

pp990

import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;

class pro extends Frame implements Runnable{

Frame f;
Thread th;
int ballx,bally,dx,dy;

public static void main(String[] args){
Frame f=new pro();
f.setSize(500,500);
f.setBackground(Color.yellow);
f.show();
}

pro() {
addWindowListener(new stopwin());
ballx=100;
bally=300;
dx=10;
dy=7;
th=new Thread(this);
th.start();
}

class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}

public void paint( Graphics g ) {
int n;
g.setColor(Color.red);
g.drawRect(ballx,bally,20,20);
ballx=ballx+dx;
bally=bally+dy;
if (ballx>500)dx=-dx;
if (ballx<0)dx=-dx;
if (bally>500)dy=-dy;
if (bally<0)dy=-dy;

}

public void update(Graphics g) {
paint(g);
}

public void run() {
int i;
for (i=1;i<1000;i++){
repaint();
try{th.sleep(100);} catch(InterruptedException e) { }
}
}


}
最終更新:2010年10月03日 08:59