import java.awt.Color;
import java.awt.Graphics;

public class Ball{
	private final static Color DEFAULT_COLOR = Color.RED; 



	int x, y;
	int dx, dy;
	int radius;
	Color color;



	Ball(){
		x     = 0;
		y     = 0;
		dx    = 0;
		dy    = 0;
		color = null;
	}



	Ball(int x, int y, int dx, int dy, int radius){
		this(x, y, dx, dy, radius, DEFAULT_COLOR);
	}



	Ball(int x, int y, int dx, int dy, int radius, Color color){
		this.x      = x;
		this.y      = y;
		this.dx     = dx;
		this.dy     = dy;
		this.radius = radius;
		this.color  = color;		
	}


	
	public void draw(Graphics g){
		g.setColor(color);
		g.fillOval(x, y, radius, radius);
	}



	public void moveOneStep(ContainerSquare square){
		/* Computing the upper and lower bound values for the
		 * x and y coordinates to detect collisions */
		int xlb = square.x + radius;
		int ylb = square.y + radius;
		int xub = (square.width-1) - radius;
		int yub = (square.height-1) - radius; 
		
		/* Moving the x and y values one step forward  */
		x += dx;
		y += dy;


		/* The ball will hit the bounderies of the square it is contained in,
		 * when the distance between the center point of the ball and the wall
		 * of the square is <= r */
		if(x < xlb){
			dx = -dx;
			x  = xlb;
		}

		if(x > xub){
			dx = -dx;
			x  = xub;
		}

		if(y < ylb){
			dy = -dy;
			y  = ylb;
		}

		if(y > yub){
			dy = -dy;
			y  = yub;
		}
	}
}
