import java.awt.*;
import java.applet.*;
public class MovingCircleApplet extends Applet implements Runnable {
protected int x;
public void init(){
x = 0; // (1) x座標の初期設定
}
public void start(){
Thread thread = new Thread(this); // (2) スレッドを生成
thread.start(); // スレッドの開始
}
public void run(){ // (3) スレッドで呼ばれる関数
while(true){
repaint(); // (4) 再描画
x += 20; // (5) x座標の更新
if(x > 300){ // 300 以上であれば 0 に戻す
x = 0;
}
try{
Thread.sleep(200); // (6) 0.2秒間スリープ
}catch(InterruptedException ex){}
}
}
public void paint(Graphics g){
g.setColor(Color.red);
g.fillOval(x, 10, 100, 100); // (7) xの位置に円を描画
}
}