import java.awt.*;

/** Do something like this and see what happens:
       b1= new Ballayl27rw98(20,30,20);
       b2= new Ballayl27rw98(30,-40,30);
       Ballayl27rw98.screenSaver(b1,b2);
       
 */
/** An instance of a ball on a JFrame. All balls use the same JFrame.<br><br>
    A ball is a visible circle drawn, with a turtle as its center, on the 
    JFrame so that it can be moved from place to place depending on the location
    of the turtle. */
public class Ballayl27rw98 extends Turtle {
  
  private int radius;     // The radius of the circle
  private int vx;         // The horitzontal velocity of the ball
  private int vy;         // The vertical velocity of the ball
  private Color save;     // The color of the ball
  
  /** = the radius of the ball */
  public int getRadius() {
    return radius;
  }
  
  /** = the hortizonal velocity of the ball */
  public int getVX() {
    return vx;
  }
  
  /** = the vertical velocity of the ball */
  public int getVY() {
    return vy;
  }
  
  /** Constructor: A ball, whose turtle starts at (x,y), with a radius
   of r, a horizontal velocity of a, a vertical velocity of b, and a
   color of c.
   Precondition: r <= x <= (500-r), r <= y <= (500-r). */
  public Ballayl27rw98(int x, int y, int a, int b, int r, Color c) {
    moveTo(x,y,0);
    vx= a;
    vy= b;
    radius= r;
    save= c;
    setColor(save);
    drawCircle(radius);
  }

  /** Constructor: A ball, whose turtle starts at the center of the panel, 
   with a radius of r, a horizontal velocity of a, a vertical velocity of 
   b, and a black color. */
  public Ballayl27rw98(int a, int b, int r) {
    moveTo(getWidth()/2,getHeight()/2,0);
    vx= a;
    vy= b;
    radius= r;
    save= Color.black;
    setColor(save);
    drawCircle(radius);
  }
  
  /** Moves the ball once from its current location. If it is about to
   hit the boundaries of the panel, it will bounce away from the wall. */
  public void moveBallOnce() {
    pause(100);
    setColor(Color.white);
    drawCircle(radius);
    if (getX()+vx+radius > getWidth() || getX()+vx-radius < 0) {
      vx= -vx;
    }
    moveTo(vx+getX(),getY(),0);
    if (getY()+vy+radius > getHeight() || getY()+vy-radius < 0) {
      vy= -vy;
    }
    moveTo(getX(),vy+getY(),0);
    setColor(save);
    drawCircle(radius);
  }
  
  /** Places the ball perpetually in motion within the confines of the
   boundaries of the panel. */
  public void inMotion() {
    while (1 != 2) {
      moveBallOnce();
    }
  }
  
  /** Places the two balls perpetually in motion within the confines of
   the boundaries of the panel. */
  public static void inMotion(Ballayl27rw98 b1, Ballayl27rw98 b2) {
    while (1 != 2) {
      b1.moveBallOnce();
      b2.moveBallOnce();
    }
  }
  
    /** Finds the color that corresponds to int c and stores it as 
      the color of the ball. */
    public void numberColor(int c) {
        if (c == 1) save= Color.black;
        if (c == 2) save= Color.blue;
        if (c == 3) save= Color.cyan;
        if (c == 4) save= Color.darkGray;
        if (c == 5) save= Color.gray;
        if (c == 6) save= Color.green;
        if (c == 7) save= Color.lightGray;
        if (c == 8) save= Color.magenta;
        if (c == 9) save= Color.orange;
        if (c == 10) save= Color.pink;
        if (c == 11) save= Color.red;
        if (c == 12) save= Color.white;
        if (c == 13) save= Color.yellow;
    }

  
  /** A screensaver that places the two filled balls perpetually in motion within 
   the confines of the boundaries of the panel. When one of the balls is completely in or on
   the other, or if they hit the boundaries of the panel, they will each loop
   through the colors in method numberColor, leaving behind their previous
   placement on the panel until pixels from the currenly moving ball overides it. */
  public static void screenSaver(Ballayl27rw98 b1, Ballayl27rw98 b2) {
    int a= 2;
    int b= 8;
    //Places ball in perpetual motion
    while (1 != 2) {
      b1.moveBallOnce();
      b2.moveBallOnce();
      b1.fillCircle(b1.radius);
      b2.fillCircle(b2.radius);
      //Changes color of ball b1 if it recently hit a boundary
      if (b1.getX()-2*b1.vx+b1.radius > b1.getWidth() || b1.getX()-2*b1.vx+b1.radius < 0 || b1.getY()-2*b1.vy+b1.radius > b1.getHeight() || b1.getY()-2*b1.vy-b1.radius < 0) {
        b1.numberColor(a);
        if (a != 13) {
          a= a+1;
        }
        else {
          a= 1;
        }
      }
      //Changes color of ball b2 if it recently hit a boundary
      if (b2.getX()-2*b2.vx+b2.radius > b2.getWidth() || b2.getX()-2*b2.vx+b2.radius < 0 || b2.getY()-2*b2.vy+b2.radius > b2.getHeight() || b2.getY()-2*b2.vy-b2.radius < 0) {
        b2.numberColor(b);
        if (b != 13) {
          b= b+1;
        }
        else {
          b= 1;
        }
      }
      int r1= b1.getX()+b1.radius;
      int l1= b1.getX()-b1.radius;
      int u1= b1.getY()+b1.radius;
      int d1= b1.getY()-b1.radius;
      int r2= b2.getX()+b2.radius;
      int l2= b2.getX()+b2.radius;
      int u2= b2.getY()+b2.radius;
      int d2= b2.getY()-b2.radius;
      //Changes colors of the balls if they were in or on each other
      if ((b1.getX() == b2.getX() && b1.getY() == b2.getY()) || (u1 <= u2 && l1 <= l2 && d2 <= d1 && r2 <= r1) || (u2 <= u1 && l2 <= l1 && d1 <= d2 && r1 <= r2)) {
        b1.vx= -b1.vx;
        b1.vy= -b1.vy;
        b2.vx= -b2.vx;
        b2.vy= -b2.vy;
        b1.numberColor(a);
        if (a != 13) {
          a= a+1;
        }
        else {
          a= 1;
        }
        b2.numberColor(b);
        if (b != 13) {
          b= b+1;
        }
        else {
          b= 1;
        }
      }
    }
  }
  
  
    
}  