//  File:  TimeAnimation.java - show an "animation in time"

//  NOTE:  This applet uses a "sleep" within the paint method...
//         While this is OK, a far better method to achieve animation
//         is to use a Timer.

//  <APPLET CODE="TimeAnimation" WIDTH="250" HEIGHT="300"> </APPLET>

import java.awt.*;
import java.applet.Applet;

public class TimeAnimation extends Applet {

    // initialize the Applet
    public void init() {
        nHeight = 0;
        setBackground(Color.blue);
    }

    public void paint(Graphics g) {
        drawBar(g, nHeight);
        // if max bar height not yet reached, calculate the next height
        if (nHeight < MAX_HGT) {
            nHeight = calcHeight(nHeight);
            sleep(200);         // sleep 0.2 seconds
            repaint();          // request a repainting
        } else
            showStatus(" Please resize the applet");
    }

    // draw a white bar of length nHeight pixels
    private void drawBar(Graphics g, int nHeight) {
        g.setColor(Color.white);
        g.fillRect(100, 25, 50, nHeight);
    }

    // calculate the length of the next bar to draw
    private int calcHeight(final int nHeight) {
        return nHeight + 5;     // increase the height by 5 pixels
    }

    // sleep (milliseconds)
    public void sleep(final int n) {
        try {
            Thread.sleep(n);
        } catch (InterruptedException e) {
            // ignore this exception
        }
    }

    private int nHeight;                    // current length of the bar in pixels
    private final int MAX_HGT = 250;        // maximum bar height

}
