import java.awt.*;
import java.awt.Color;


/** Interesting thing: Create a Balldah64 using something like
 
       x= new Balldah64(65,40,20);
       
    execute the following procedure call to see something like a
    hockey ball puck around --when it hits the goal, a red light blinks.
*/
public class Balldah64 extends Turtle{
  private int radius;
  private int vx;
  private int vy;
  
  /** "= radius of the ball" */
  public int getRadius(){
    return radius;
  }
  
  /** "= acceleration in x-direction of the ball" */
  public int getVx(){
    return vx;
  }
  
  /** "= acceleration in y-direction of the ball" */
  public int getVy(){
    return vy;
  }
  
  /** Constructor for class Ball.  Ball starts at position (x,y), has acceleration 
   *  (vx, vy), has radius r and is color c */
  public Balldah64(int x, int y, int vX, int vY, int r, Color c){ 
    moveTo(x, y, 0);
    setColor(c);
    vx= vX;
    vy= vY;
    radius= r;
    fillCircle(radius*2);
  }
  
  /** Constructor for class Ball.  Ball starts at midpoint of panel, has acceleration
   *  (vx, vy), radius r, and is black. */
  public Balldah64(int vX, int vY, int r){
    moveTo(getWidth()/2,getHeight()/2, 0);
    setColor(Color.black);
    vx= vX;
    vy= vY;
    radius= r;
    fillCircle(radius*2);
  }
  
  /** Constructor: a ball with radius r starting at (x,y)
   *  at angle 0, velocity (vx, vy), and color c as given in Turtle.tColor */
  public Balldah64 (int x, int y, int vX, int vY, int r, int c){
    moveTo(x, y, 0);
    tColor(c);
    vx= vX;
    vy= vY;
    radius= r;
    fillCircle(radius*2);
  }
  
  /** Constructor: a ball with radius r starting at the midpoint
   *  at angle 0, velocity (vx, vy), and color c as given in Turtle.tColor*/
  public Balldah64 (int vX, int vY, int r, int c){
    moveTo(getWidth()/2,getHeight()/2, 0);
    tColor(c);
    vx= vX;
    vy= vY;
    radius= r;
    fillCircle(radius*2);
  }
  
  
  
  /** Moves the ball once as given by the acceleration (vx, vy) of the ball
   * If the ball hits a "wall" of the screen, it will bounce off it.*/
  public void moveBallOnce(){
    if(getRadius() > getY()) vy= -vy;
    if(getRadius() > getHeight()-getY()) vy= -vy;
    if(getRadius() > getX()) vx= -vx;
    if(getRadius() > getWidth()-getX()) vx= -vx;
    Color save= getColor();
    int newX= getX() + getVx();
    int newY= getY() + getVy();
    Balldah64 blank= new Balldah64(getX(), getY(), getVx(), getVy(), getRadius(), Color.white);
    moveTo(newX, newY, 0);
    Balldah64 moved= new Balldah64(newX, newY, getVx(), getVy(), getRadius(), save);
  }
  
  /** Puts the ball in continuous motion.  Again, it moves as given by its acceleration
   *  (vx, vy) and bounces off the walls of the screen*/
  public void inMotion(){
    for (int i= 2; i > 1; i++){
      moveBallOnce();
      pause(100);
    }
  }
  /** Puts balls b1 and b2 in motion. */  
  public static void inMotion(Ball b1, Ball b2){
    for (int i= 2; i > 1; i++){
      b1.moveBallOnce();
      b2.moveBallOnce();
      b1.pause(100);
      b2.pause(100);
    }
  }
  
  /** draws a hockey "net" (a black rectangle) and puts a created ball in motion.  The ball
   *  bounces like normal except if you score (the ball hits the net), the ball stops moving and
   *  the red light comes on, signifying a goal. */
  public void hockey(){
    int saveX= getX();
    int saveY= getY();
    moveTo(0, (getHeight()/2), 270);
    fillRectangle(20, 150);
    moveTo(saveX, saveY, 0);
    for (int i= 2; i > 1; i++){
      moveBallOnce();
      pause(100);
      if((getRadius() > getX() - 20) && (getY() > (getHeight()/2)-75) 
           && (getY() < ((getHeight()/2)+75))){
        vx= 0;
        vy= 0;
        moveTo(getWidth()-50, getHeight()-40, 0);
        setColor(Color.red);
        fillCircle(30);
      }
    }
  }
}


