/**
* @author Amanda Gorski
* @date July 7, 2002
*
* Requires Bouncer.java, run it there
*/
import java.awt.*;
import javax.swing.*;
import java.text.*;

public class Ball extends JComponent implements Runnable
{
    private volatile int x; //present co-ordinate
    private volatile int y; //present co-ordinate
    private volatile boolean dir_x; //true equals right
    private volatile boolean dir_y; //true equals up
    private volatile boolean stopped; //true if the ball isn't bouncing
    private int bound_x; //how big the screen is in the x direction
    private int bound_y; //how big the screen is in the y direction
    private int speed; //how fast the ball is moving

    public Ball (int xx, int yy)
    { //Pre: xx is how big the screen is in the x direction, yy for the y direction
	//Post: a new ball is constructed and initialized
	x = 150;
	y = 82;
	dir_x = true;
	dir_y = false;
	bound_x = xx;
	speed = 7;
	bound_y = (yy);
	setBackground (Color.white);
    }


    public void run ()
    { //run method, starts when "Bounce" button is pressed
	moveBall ();
    }


    public void stopBall ()
    { //to pause the thread in the moveball method, stopped must be true
	//this stops the loop in moveball
	stopped = true;
    }


    public void moveBall ()
    { //calculates the new position of the ball and moves it
	//repeats until the user clicks the stop button.
	stopped = false;

	while (!stopped)
	{

	    drawBall ();

	    //choose new position
	    if (dir_x) //moving right
	    {
		// check bounds of window
		if (x < (bound_x - 10))
		    x += speed;
		else
		{
		    dir_x = false;
		    x -= speed;
		}

		// check bounds of box - left side
		if (x > 100 && y > (100 - 10) && y < 250 && x < (250 - 10))
		{
		    dir_x = false;
		    x -= speed;
		}

		// check bounds of box2 - left side
		if (x > 250 && y > (250 - 10) && y < 300 && x < (300 - 10))
		{
		    dir_x = false;
		    x -= speed;
		}
	    }
	    else //moving left
	    {
		// check bounds of window
		if (x > 5)
		    x -= speed;
		else
		{
		    dir_x = true;
		    x += speed;
		}
		// check bounds of box - right side
		if (x < 250 && y > (100 - 10) && y < 250 && x > 100)
		{
		    dir_x = true;
		    x += speed;
		}

		// check bounds of box2 - right side
		if (x < 300 && y > (250 - 10) && y < 300 && x > 250)
		{
		    dir_x = true;
		    x += speed;
		}
	    }

	    if (dir_y) //moving up
	    {
		if (y < (bound_y - 10))
		    y += speed;
		else
		{
		    dir_y = false;
		    y -= speed;
		}

		// check bounds of box - top
		if (y > 100 && x > (100 - 10) && x < 250 && y < (250 - 10))
		{
		    dir_y = false;
		    y -= speed;
		}

		// check bounds of box2 - top
		if (y > 250 && x > (250 - 10) && x < 300 && y < (300 - 10))
		{
		    dir_y = false;
		    y -= speed;
		}
	    }
	    else //moving down
	    {
		if (y > 5)
		    y -= speed;
		else
		{
		    dir_y = true;
		    y += speed;
		}

		// check bounds of box - bottom
		if (y < 250 && x > (100 - 10) && x < 250 && y > 100)
		{
		    dir_y = true;
		    y += speed;
		}

		// check bounds of box2 - bottom
		if (y < 300 && x > (250 - 10) && x < 300 && y > 250)
		{
		    dir_y = true;
		    y += speed;
		}

	    }


	    //sleep for a bit after choosing new position
	    try
	    {
		Thread.sleep (50);
	    }
	    catch (InterruptedException x)
	    {   
		;
	    }

	}
    }


    public void drawBall ()
    {
	repaint ();
    }


    /** draw the ball on the screen
    */
    public void paint (Graphics g)
    {
	//Draw a white square on screen to erase old ball
	g.setColor (Color.white);
	//g.fillRect (x - 2 * speed, y - 2 * speed, 4 * speed, 4 * speed);
	//Choose a colour based on position
	int b = (int) x / 2;
	int gr = (int) y / 3;
	g.setColor (new Color (0, b, gr));
	//Draw the new ball
	g.fillOval (x - 5, y - 5, 10, 10);
	g.fill3DRect (100, 100, 150, 150, true);
	g.setColor (new Color (0, gr, b));
	g.fill3DRect (250, 250, 50, 50, true);
    }



}
