import java.awt.*;  //abstract windowing toolkit - contains graphics
import javax.swing.*;  
import java.awt.event.*;

// Note: Could have subclassed Canvas and added the canvas to the frame as well

public class ImprovedAnimationPanel extends JPanel implements ActionListener {

	Timer timer;
	// Flip changes the direction of the growth of the ball
	boolean flip=true;
	// Location of the ball
	public int i=15, j=15;
	// Size of the ball
	private int x=0,y=0;
		
	// Constructor
	public ImprovedAnimationPanel()
	{
		// Method of JComponent
		setBackground(Color.blue);
		setSize(new Dimension(400,400));
		timer = new Timer(100, this);
		timer.start();
	}
	
	public ImprovedAnimationPanel (int time)
	{
		// Method of JComponent
		setBackground(Color.blue);
		setMinimumSize(new Dimension(150,200));
		timer = new Timer(time, this);
		timer.start();
	}

	// Use g to draw on the Applet, lookup java.awt.Graphics in
	// the javadoc to see more of what this can do for you!!

	//!! THIS IS NOT THE OO APPROACH!!

	public void paintComponent(Graphics g) 
	{
        super.paintComponent(g); //paint background
		g.fillOval(i,j,x+5,x+5);
	}
	
	public void actionPerformed(ActionEvent e){ 
		// Move ball
		if (flip)
		{
			x++;
			y++;
			if (x==10)
				flip=!flip;
		}
		else
		{
			x--;
			y--;
			if (x==0)
				flip=!flip;
		}	
		repaint();  // Schedule a repaint of screen
	}
}

