import java.awt.*;
import java.applet.*;

public class MovingCircleApplet2 extends MovingCircleApplet {

    protected int oldX;                     // (1) 1つ前の円の位置

    public void init(){
        x = 0;
        oldX = 0;
    }

    public void run(){
        while(true){
            repaint();

            oldX = x;                       // (2) 現在の円の位置を
                                            //     保存しておく
            x += 20;
            if(x > 300){
                x = 0;
            }

            try{
                Thread.sleep(200);
            }catch(InterruptedException ex){}
        }
    }

    public void update(Graphics g){        // (3) update を再定義して
        paint(g);                          //     背景色での全面消去を
    }                                      //     行わないようにする

    public void paint(Graphics g){
        g.clearRect(oldX, 10, 100, 100);   // (4) 前の位置の円を消去する

        g.setColor(Color.red);
        g.fillOval(x, 10, 100, 100);       // (5) xの位置に円を描画
    }
}