import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JPanel;


public class BouncingBallPanel extends JPanel{
	private final static int FRAME_RATE = 40;

	private int containerPanelWidth;
	private int containerPanelHeight; 
	private Ball ball;
	private ContainerSquare square;
	private ContainerPanel cp;

	
	BouncingBallPanel(){
		containerPanelWidth  = 0;
		containerPanelHeight = 0;
		ball                 = null;
		square               = null;
		cp 			         = null;
	}


	
	BouncingBallPanel(int width, int height){
		containerPanelWidth  = width;
		containerPanelHeight = height;

		ball = new Ball(0, 0, 2, 2, 15);
		square = new ContainerSquare(0, 0, containerPanelWidth, containerPanelHeight);
		cp = new ContainerPanel();
		this.setLayout(new BorderLayout());
		this.add(cp, BorderLayout.CENTER);

		startBouncing();
	}



	public void startBouncing(){
		Thread bounceThread = new Thread(){
			public void run(){
				while(true){
					ball.moveOneStep(square);
					repaint();
		
					try{
						Thread.sleep(1000 / FRAME_RATE);
					} catch(InterruptedException ie){}
				}
			}
		};
		bounceThread.start();
	}



	class ContainerPanel extends JPanel{
		@Override
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			square.draw(g);
			ball.draw(g);
		}		


		@Override
		public Dimension getPreferredSize(){
			return (new Dimension(containerPanelWidth, containerPanelHeight));
		}
	}
}
