/** Interesting thing. A call like 
 
      Ballamh82.rectangles(20, 30, 40, 10);
      
     creates a bunch of rectangles at the top and one ball.
     The ball bounces around. When it hits a rectangle, the
     rectangle becomes black.  */
public class Ballamh82 extends Turtle {
  private int radius;
  private int vx;
  private int vy;
  
  /** = radius of 
   */
  public int getRadius() {
    return radius; 
  }
  
  /** = acceleration in x direction of ball */
  public int getVx() {
    return vx;
  }
  
  /** = acceleration in y direction of ball */
  public int getVy() {
    return vy; 
  }
  
  /** constructor: creates a ball starting at coord. (x, y),
   * with acceleration (vx1, vy1), and color c */
  public Ballamh82(int x, int y, int vx1, int vy1, int r, java.awt.Color c) {
    this.moveTo(x, y, 0);
    vx= vx1;
    vy= vy1;
    radius= r;
    this.moveTo(x, y, 0);
    this.setColor(c);
    this.fillCircle(2*r);
  }
  
  /** constructor: creates a ball starting in the center of the applet
   * has acceleration (vx1, vy1), radius r, and is black */
  public Ballamh82(int vx1, int vy1, int r) {
    int x= this.getWidth() / 2;
    int y= this.getHeight() / 2;
    this.moveTo(x, y, 0);
    vx= vx1;
    vy= vy1;
    radius= r;
    this.moveTo(x, y, 0);
    this.setColor(java.awt.Color.black);
    this.fillCircle(2*r);   
  }
  
  /* move a previously created ball */
  public void moveBallOnce() {
    int x= this.getX();  //current x coordinate of ball at procedure call
    int y= this.getY();  // current y coordinate of ball at procedure call
    int r= this.getRadius(); // radius of ball 
    int i= 0;   //pixels the turtle of the ball should move in the x direction
    int z= 0;   // pixels the turtle of the ball should move in the y direction
    int vx1= this.getVx();  //the acceleration in the x direction of ball at time of procedure call
    int vy1= this.getVy();  //the acceleration in the y direction of ball at the time of procedure call
    java.awt.Color save= this.getColor();
    while (vx1 != 0 || vy1 != 0) {
      this.setColor(java.awt.Color.white);
      this.fillCircle(2*r);
      if (this.getY() < r){
        vy1= -vy1;
        vy= -vy;
      }
      if (this.getX() < r ) {
        vy1= -vy1;
        vx= -vx;
      }
      if (this.getHeight() < this.getY() + r) {
        vy1= -vy1;
        vy= -vy;
      }
      if (this.getWidth() < this.getX() + r) {
        vy1= -vy1;
        vx= -vx;
      }
      if ( vx1 < 0 ) {
        i= i - 1;
        vx1= vx1 + 1;
      }
      if (vx1 > 0) {
        i= i +1;
        vx1= vx1 - 1;
      }
      if ( vy1 < 0) {
        z= z - 1;
        vy1= vy1 + 1;
      }
      if ( vy1 > 0) {
        z= z+1;
        vy1= vy1 -1;
      }
      this.moveTo(x+i, y+z, 0);
      this.setColor(save);
      this.fillCircle(2*r);
      pause(100);     
      
    }
    
    this.setColor(save);
    this.fillCircle(2*r); 
  }
  
  /** sets ball in perptual motion */
  public void inMotion() {
    int i= 0;
    while (i != 1){
      this.moveBallOnce();
    }
    
  }
  
  /** static method: sets ball b1 and ball b2 in perpetual motion */
  public static void inMotion(Ballamh82 b1, Ballamh82 b2) {
    int i= 0;
    while(i != 1) {
      b1.moveBallOnce();
      b2.moveBallOnce();
    }
    
  }
  
  /** static method: creates 5 rectangles with height h and
   * creates a black ball starting in the center with acceleration (vx1, vy1)
   * and radius r.  Sets the ball in motion, rectangle turns black when ball hits it
   * and ball changes direction */
  public static void rectangles(int h, int vx2, int vy2, int r) {
    Ballamh82 ball= new Ballamh82(vx2, vy2, r);  //the ball to be used
    MyTurtle rect= new MyTurtle();  // turtle that draws rectangles
    int q= 0;   // value determining when the loop that creates rectangles stops
    int w= 0;   //value determining when the loop that moves the ball stops (infinite loop)
    int length= rect.getWidth();  // length of the panel
    int a= (length/5) / 2;   // half the width of the rectangle (changes)
    int e= (length/5) / 2;  // half the width of the rectangle (constant)
    int l= (length/5);   //width of rectangle
    rect.setColor(java.awt.Color.green);
    while( q != 5 ){
      rect.moveTo(a, h/2, 0);
      rect.fillRectangle(length/5, h);
      if (rect.getColor().equals(java.awt.Color.green))
        rect.setColor(java.awt.Color.blue);
      else rect.setColor(java.awt.Color.green);
      
      a= a + l;
      q= q+1;
    }
    while (w != 1){
      int x= ball.getX();  //x position of ball at loop execution
      int y= ball.getY();  // y position of ball at loop execution
      int i= 0;    // # of pixels ball is moved in the x direction
      int z= 0;   // # of pixels ball is moved in the y direction
      int vx1= ball.getVx();  // acceleration of ball in x direction at loop execution
      int vy1= ball.getVy();  // acceleration of ball in y direction at loop execution
      while (vx1 != 0 || vy1 != 0) {
        ball.setColor(java.awt.Color.white);
        ball.fillCircle(2*r);
        if (ball.getX() <= r ) {
          vx1= -vx1;
          ball.vx= -ball.vx;
        }
        if (ball.getHeight() <= ball.getY() + r) {
          vy1= -vy1;
          ball.vy= -ball.vy;
        }
        if (ball.getWidth() <= ball.getX() + r) {
          vx1= -vx1;
          ball.vx= -ball.vx;
        }
        if (ball.getY() <= r+h && ball.getX() <= l){
          rect.moveTo(e, h/2, 0);
          rect.setColor(java.awt.Color.black);
          rect.fillRectangle(length/5, h);
          vy1= -vy1;
          ball.vy= -ball.vy;
        }
        if (ball.getY() <= r+h && ball.getX() >= l && ball.getX() <= 2*l){
          rect.moveTo(e + l, h/2, 0);
          rect.setColor(java.awt.Color.black);
          rect.fillRectangle(length/5, h);
          vy1= -vy1;
          ball.vy= -ball.vy;
        }
        if (ball.getY() <= r+h && ball.getX() >= 2*l && ball.getX() <= 3*l){
          rect.moveTo(e + 2*l, h/2, 0);
          rect.setColor(java.awt.Color.black);
          rect.fillRectangle(length/5, h);
          vy1= -vy1;
          ball.vy= -ball.vy;
        }
        if (ball.getY() <= r+h && ball.getX() >= 3*l && ball.getX() <= 4*l){
          rect.moveTo(e + 3*l, h/2, 0);
          rect.setColor(java.awt.Color.black);
          rect.fillRectangle(length/5, h);
          vy1= -vy1;
          ball.vy= -ball.vy;
          
        }
        if (ball.getY() <= r+h && ball.getX() >= 4*l && ball.getX() <= length){
          rect.moveTo(e + 4*l, h/2, 0);
          rect.setColor(java.awt.Color.black);
          rect.fillRectangle(length/5, h);
          vy1= -vy1;
          ball.vy= -ball.vy;
          
        }
        if ( vx1 < 0 ) {
          i= i - 1;
          vx1= vx1 + 1;
        }
        if (vx1 > 0) {
          i= i +1;
          vx1= vx1 - 1;
        }
        if ( vy1 < 0) {
          z= z - 1;
          vy1= vy1 + 1;
        }
        if ( vy1 > 0) {
          z= z+1;
          vy1= vy1 -1;
        }
        ball.moveTo(x+i, y+z, 0);
        ball.setColor(java.awt.Color.black);
        ball.fillCircle(2*r);
        ball.pause(10);     
        
      }
    }
  }
}

