import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

/**
 * A simple example of ad-hoc animation
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AnimatedRectangle extends JComponent implements ActionListener
{
    private float        direction, height, width, x, y;
    private Line2D.Float ground;    
    private Timer        timer;


    /**
     * Default Constructor
     */
    public AnimatedRectangle()
    {
	super();

	width = 50.0f;
	height = 50.0f;

	x = 100.0f;
	y = 100.0f;
	direction = 1.0f;

        ground = new Line2D.Float(0f,480f,480f,480f);        

	timer = new Timer(10, this);
	timer.start();
    }



    /**
     * Handle actionPerformed messages generated by the Timer
     * (Required by ActionListener)
     *
     * @param evt    The ActionEvent
     */
    public void actionPerformed(ActionEvent evt)
    {
	if      (y <= 100)        direction = 1.0f;
	else if (y+height >= 480) direction = -1.0f;

	y += direction;

	repaint();
    }




    /**
     * Render this Painting
     *
     * @param g   The rendering engine to use
     */
    public void paint(Graphics g)
    {
       Graphics2D            g2;       
       Rectangle2D.Float     rectangle;

       g2 = (Graphics2D)g;
       g2.setColor(Color.BLACK);       
       g2.draw(ground);
       
       g2.setColor(Color.RED);       
       rectangle = new Rectangle2D.Float(x, y, width, height);
       g2.draw(rectangle);

    }
}
