// *********************************************************************
// Ball.java
// This is a generic "ball" that can move and reposition itself if it goes
// off-screen.
//
// Murphy Stein
// NYU Computer Graphics
// 2006
// *********************************************************************
import java.awt.*;

public class Ball
{

	int x, y;
	int dx, dy;
	int steps;
	Color myColor;
	
	int everlasting;
	
	int w = 5, h = 5;
	
	public Ball( ) {
		initialize();
		this.everlasting = 1;
	}
	
	public Ball(int nx, int ny, int everlasting, Color c) {
		x = nx;
		y = ny;
		pickRandomDirection();
		this.everlasting = everlasting;
		myColor = c;
	}
	
	public void initialize( )
	{
		pickRandomDirection();
		steps = 0;
		float c = (float)(Math.random());
		myColor = new Color (c, c, c);
	}
	
	public void move() {	
		x += dx;
		y += dy;
		if (OutOfBounds()) {
			if (everlasting == 1) {
				initialize();
			}
		}
		steps++;
	}

	public void draw(Graphics g) {
		if (OutOfBounds() == false) {
			g.setColor( myColor );	
			g.drawLine( x, y, x + dx , y + dy );
		}
	}
	
	public boolean OutOfBounds() {
		return ((x > 500) || (x < 0) || (y > 500) || (y < 0) || (steps > 100));
	}
	
	public void pickRandomDirection() {
		dx = (int)(Math.random() * 2 * 50) - 50;
		dy = (int)(Math.random() * 2 * 50) - 50;
		if ((dx == 0) && (dy == 0)) {
			pickRandomDirection();
		}
	}
	
}