//  File:  TimeAnimationT.java - show an "animation in time"

//  NOTE:  This applet uses a timer to achieve animation
//         This is a far better technique than just to use
//         a sleep() within the paint method.

//  <APPLET CODE="TimeAnimationT" WIDTH="250" HEIGHT="300"> </APPLET>

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.Timer;

public class TimeAnimationT extends Applet {

    // initialize the Applet
    public void init() {
        nHeight = 0;
        setBackground(Color.blue);
        timer = new Timer(DELAY, new TimerActionListener());
        timer.start();
    }

    public void paint(Graphics g) {
        drawBar(g, nHeight);
        showStatus(" Please resize the applet");
    }

    // providing this method reduces "flicker"
    public void update(Graphics g) {
        paint(g);
    }

    // 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
    }

    // implements the action listener for the Timer.
    private class TimerActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            // if max bar height not yet reached, calculate the next height
            if (nHeight < MAX_HGT)
                nHeight = calcHeight(nHeight);
            // otherwise, stop the timer
            else
                timer.stop();
            repaint();
        }
    }

    private int nHeight;                    // current length of the bar in pixels
    private final int MAX_HGT = 250;        // maximum bar height
    private final int DELAY = 50;            // delay in milliseconds
    private Timer timer;                    // the timer itself

}
