import javax.swing.*;
import java.awt.*;
/** Assignment 4 task 4: Bouncing Balls 
 * 
    Execute the following to create 4 rectangles on
    the top of the window and have them disappear when
    the ball hits them.
    
    b1= new Ballml363(10, 20, 15);
    b1.killRectangles();
*/
public class Ballml363 extends Turtle{
  private int radius;
  private int vx;
  private int vy;
  private boolean[] rect = new boolean[4];
  
  /** 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 Ballml363(int x, int y, int vx, int vy, int r, Color c){
    setColor(c);
    radius = r;
    this.vx = vx;
    this.vy = vy;
    moveTo(x,y,0);
    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 Ballml363 (int x, int y, int vx, int vy, int r, int c){ 
    tColor(c);
    radius = r;
    this.vx = vx;
    this.vy = vy;
    moveTo(x,y,0);
    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 Ballml363 (int vx, int vy, int r, int c){ 
    tColor(c);
    radius = r;
    this.vx = vx;
    this.vy = vy;
    moveTo(getWidth()/2,getHeight()/2,0);
    fillCircle(radius*2);
  }
    
  /** Constructor: a ball with radius r starting at the midpoint
   *  at angle 0, velocity (vx, vy), */
   public Ballml363(int vx, int vy, int r){
    radius = r;
    this.vx = vx;
    this.vy = vy;
    moveTo(getWidth()/2,getHeight()/2,0);
    fillCircle(radius*2);
  }
   
   // = vx of ball
   public int getVx(){
     return vx;
   }
   // = vy of ball
   public int getVy(){
     return vy;
   }
   
   // change the value of Vx to v
   public void setVx(int v){
     vx = v;
   }
   
   // change the value of Vy to v
   public void setVy(int v){
     vy = v;
   }
   
   /* moves ball once by acceleration of vx, vy.
    * once hits a wall, velocity of ball reverses. */
   public void moveBallOnce(){
     Color save = getColor();
     if(vy >= 0 && getY() + vy + radius > getHeight() || vy < 0 && getY() + vy - radius < 0)
       vy = -vy;
     if(vx >= 0 && getX() + vx + radius > getWidth() || vx < 0 && getX() + vx - radius < 0)
       vx = -vx;  
     setColor(Color.white);
     fillCircle(radius*2);
     moveTo(getX()+vx, getY()+vy, 0);
     setColor(save);
     fillCircle(radius*2);
   }
   
   /** moves ball perpetually by acceleration of vx and vy */
   public void inMotion(){
     for(int i=0; i>=0; i++){
       moveBallOnce();
       pause(100);
     }
   }
  
   /** static. moves ball b1 and b2 in motion forever */
   public static void inMotion(Ballml363 b1, Ballml363 b2){
     for(int i=0; i>=0; i++){
       b1.moveBallOnce();
       b2.moveBallOnce();
       b1.pause(100);
       b2.pause(100);
     }
   }
   
   /** creates 4 rectangles of 100x20 dimension  
    * that has: 25 units between first rectangle on the left and the left wall; 5 units between all rectangles and the top wall; 
    * and 10 units between each rectangle  */
   public void createRectangles(){
     int tempX = getX();
     int tempY = getY();
     Color tempColor = getColor();
     setColor(Color.red);
     for(int i=0; i<4; i++){
       moveTo(75+110*i, 15, 0);
       rect[i] = true;
       fillRectangle(100,20);
     }
     moveTo(tempX, tempY, 0);
     setColor(tempColor);
   }
 
   /** moves ball by acceleration vx and vy perpetually
    * once ball hits a rectangle, velocity of ball reverses and the rectangle dissappears
    * once hits a wall, velocity of ball reverses. */
   public void killRectangles(){
    createRectangles();
    for(int i=0; i>=0;i++){
     Color tempColor = getColor();
     int tempX = getX();
     int tempY = getY();
     if(getY() - radius + vy <= 25+radius){
       for(int j=0; j<rect.length; j++){       
         if(rect[j] == true && getX() + vx + radius > j*110+25 && getX()+vx-radius < (j+1)*110+25){     
           rect[j] = false;  
           setColor(Color.white);
           moveTo(75+j*110,15,0);
           fillRectangle(100,20);
           vx = -vx;
           vy = -vy;
          }
        }
      }
     
     if(vy >= 0 && getY() + vy + radius > getHeight() || vy < 0 && getY() + vy - radius < 0){
       vy = -vy;}
     if(vx >= 0 && getX() + vx + radius > getWidth() || vx < 0 && getX() + vx - radius < 0){
       vx = -vx;}  
       
     moveTo(tempX, tempY, 0);
     setColor(Color.white);
     fillCircle(radius*2);
     setColor(tempColor);
     moveTo(tempX + vx,tempY+vy, 0);
     fillCircle(radius*2);
     pause(50);
    }
     
  }
}  

    
    
    
  
  