import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * This panel represents a panel (i.e. a rectangular area on the screen) where
 * an animation is displayed.  The drawFrame method draws one frame of the
 * animation.  To get a different animation, you just have to change this method.
 */
public class BasicAnimationPanel extends JPanel implements ActionListener {
	
	/**
	 * This method draws one frame of the animation.  The rest of the BasicAniamtionClass
	 * is called by the system each time a frame needs to be drawn. There is a variable
	 * called  frameNumber that tells you which frame you are supposed to draw.  frameNumber
	 * starts out at 0, goes up by one each time this subroutine is called until it reaches
	 * 99, and then returns to zero to repeat the animation sequence over and over.
	 * (The upper limit can be changed or eliminated -- see the next subroutine.)
	 * By using frameNumber in your drawing, the picture will change from one frame to
	 * the next, and the user will see an animated image.  The size of the drawing
	 * area is supposed to be 500-by-500 pixels (but this can also be changed in the
	 * next subroutine).
	 *    For drawing, you can use the subroutines g.setColor(), g.drawString(),
	 * g.drawLine(), g.drawRect(), g.fillRect(), g.drawOval(), and g.fillOval()
	 * These subroutines are part of the object g of type Graphics, which is a 
	 * parameter to this subroutine.  (This is the "good" way to do graphics.)
	 */
	private void drawFrame(Graphics g, int frameNumber) {

		int circleSize = 5*frameNumber;      // Circle increases in size.
		g.setColor(Color.YELLOW);
		g.fillOval(0,0,circleSize,circleSize);
		
		int squareLeft = -100 + 6*frameNumber;  // Square move left to rignt.
		g.setColor(Color.BLACK);
		g.fillRect(squareLeft,200,100,100);
		
		int x1,y1,x2,y2;  // For the mathematically inclined, a rotating line.
		x1 = 250 + (int)(200*Math.cos((frameNumber/100.0)*2*Math.PI));
		y1 = 250 + (int)(200*Math.sin((frameNumber/100.0)*2*Math.PI));
		x2 = 250 - (int)(200*Math.cos((frameNumber/100.0)*2*Math.PI));
		y2 = 250 - (int)(200*Math.sin((frameNumber/100.0)*2*Math.PI));
		
		g.setColor(Color.red);
		g.drawLine(x1,y1,x2,y2);
		
	}
	
	/**
	 * This is the "constructor" that is called to create an object of type
	 * BasicAnimationPanel.  It sets up all the initial properties of the object.
	 * You can change some of the statements here, as noted, to modifiy the
	 * behavior of the animation.  Note in particular setPreferredSize,
	 * setBackground, and frameCount.
	 *
	 */
	public BasicAnimationPanel() {
		setPreferredSize( new Dimension(500,500) );  // The size should be 50o-by-500.
		setBackground(Color.WHITE);   // Background color for the window.
		setFont(new Font("Serif",Font.PLAIN,12));  // Initial font size is 12.
		frameCount = 100; // Number of frames before frameNumber returns to 0 and animation 
		                  // repeats.  If set to 0, frameNumber keeps increasing forever. 
		frameNumber = -1; // (Do not change.)
		Timer timer = new Timer(50,this);  // 50 milliseconds between frames
		timer.setInitialDelay(1500);        // one second before animation starts
		timer.start();
	}
	
	
	//---- implementation details -- no need to read or understand past this point ----
	
	private int frameNumber;
	private int frameCount;
	
	public void actionPerformed(ActionEvent evt) {
		frameNumber++;
		if (frameNumber >= frameCount)
			frameNumber = 0;
		repaint();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (frameNumber >=0)
			drawFrame(g,frameNumber);
	}
	
	
} // end BasicAnimation
