//KONG Pui Cheong Ass7
import java.awt.*;

public class BilliardBall
{
    private BilliardTable t;
    private Color bc;
    private int bw;
    private int x, y;
    private int x1, y1, x2, y2; 

    public BilliardBall (BilliardTable t, Color bc, int bw)
	{
	this.t=t;
	this.bc=bc;
	this.bw=bw;
	}

    public static void pause (int n)
    {
        try { Thread.sleep (n); } catch (InterruptedException e) {}
    }
    

    public void roll (Graphics g)
	{
	x1=t.getTablex()+10; 
	y1=t.getTabley()+10; 
	x2=t.getTablex()+t.getTablew()-10-bw; 
	y2=t.getTabley()+t.getTableh()-10-bw; 

	x = (int)(   Math.random() * (x2-x1+1) + x1+1);
	y = (int)(   Math.random() * (y2-y1+1) + y1+1);
	g.setColor(bc);
	g.fillOval(x,y,bw,bw);
	
	int dr = 1; 
	for(int i=1 ; i<=1500 ; i++)
	{
		
		switch (dr)
		{
		case 1:
		if( x >= x2)
		dr=2;
		if( y <= y1 )
		dr=4;
		if( x >= x2 && y <= y1)
		dr=3;
		break;

		case 2:
		if( x <= x1 )
		dr=1;
		if( y <= y1 )
		dr=3;
		if( x <= x1 && y <= y1)
		dr=4;
		break;

		case 3:
		if( x <= x1 )
		dr=4;
		if( y >= y2 )
		dr=2;
		if( x <= x1 && y >= y2)
		dr=1;
		break;

		case 4:
		if( x >= x2 )
		dr=3;
		if( y >= y2 )
		dr=1;
		if( x >= x2 && y >= y2)
		dr=2;
		break;
		}
		
		g.setColor( t.getTablec () );
		g.fillOval(x,y,bw,bw);

		switch (dr)
		{
		case 1: 
		x++;
		y--;
		break;

		case 2: 
		x--;
		y--;
		break;

		case 3: 
		x--;
		y++;
		break;

		case 4: 
		x++;
		y++;
		break;
		}
				
		g.setColor(bc);
		g.fillOval(x,y,bw,bw); 

		pause (4);

	} 
 } 
} 

