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 AnimatedCurve extends JComponent implements ActionListener
{
    private CubicCurve2D.Float   curve;
    private float[]              x, y;
    private Stroke               curveStroke;
    private Timer                timer;


    /**
     * Default Constructor
     */
    public AnimatedCurve()
    {
       super();

       curveStroke   = new BasicStroke(2.0f, 
                                       BasicStroke.CAP_ROUND,
                                       BasicStroke.JOIN_MITER);

       setBackground(Color.white);


       x = new float[4];
       y = new float[4];

       x[0] =   0.0f;   y[0] =   0.0f;
       x[1] = 100.0f;   y[1] = 100.0f;
       x[2] = 300.0f;   y[2] = 300.0f;
       x[3] = 400.0f;   y[3] = 400.0f;
	
       curve = new CubicCurve2D.Float();
       updateCurve();

       timer = new Timer(100, this);
       timer.start();
    }



    /**
     * Handle actionPerformed messages generated by the Timer
     * (Required by ActionListener)
     *
     * @param evt    The ActionEvent
     */
    public void actionPerformed(ActionEvent evt)
    {
       x[0] += 1.0f;
       y[0] += 1.0f;

       x[1] += 1.0f;
       y[1] -= 1.0f;

       x[2] -= 1.0f;
       y[2] += 1.0f;

       x[3] -= 1.0f;
       y[3] -= 1.0f;

       updateCurve();
       repaint();
    }




    /**
     * Render this Painting
     *
     * @param renderer   The rendering engine to use
     */
    public void paint(Graphics   g)
    {
       Graphics2D     g2;
       
       g2 = (Graphics2D)g;

       g2.setColor(Color.black);
       
       if (curveStroke != null) g2.setStroke(curveStroke);
       
       if (curve != null) g2.draw(curve);
    }



    /**
     * Update the points in the curve
     */
    private void updateCurve()
    {
       curve.setCurve(x[0],y[0],x[1],y[1],
                      x[2],y[2],x[3],y[3]);
    }

}
