/** Interesting procedure: flags() -> draws several nations' flags in perpetuity.
 * Spent: approximately 8 hours on the assignment. */
import java.awt.*;
/** Creates a bouncing ball */
public class Ball extends YourTurtle {
  
  private int radius; //radius of the ball
  private int vx; //speed of ball (horizontal)
  private int vy; //speed of ball (vertical)
  
  /** Constructor: a new Ball with a turtle at 
   * position (x, y) and has speed (vx, vy), radius r,
   * and color c */
  public Ball(int x, int y, int vx, int vy, int r, int c){
    this.moveTo(x,y,0);
    this.vx = vx;
    this.vy = vy;
    this.radius = r;
    this.setColor(c);
    this.fillCircle(radius*2);
  }
  
  /** Constructor: a new Ball with a turtle at the middle, 
   * speed (vx, vy), radius r and color black */
  public Ball (int vx, int vy, int r){
    this(width/2, height/2, vx, vy, r, 1);
  }
  
  /** Returns the radius of the ball */
  public double getRadius(){
    return radius;
  }
  
  /** Returns the horizontal speed of the ball */
  public int getVX(){
    return vx; 
  }
  
  /** Returns the vertical speed of the ball */
  public int getVY(){
    return vy;
  }
  
  /** Erases the ball, moves it, and then
   * redraws it. */
  public void moveBallOnce(){
    Color save = this.getColor();
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setAngle(0);
    this.move(vx);
    this.setAngle(90);
    this.move(vy); 
    this.setColor(save);
    this.fillCircle(radius*2);
    if(this.getY() < radius)
      vy = -vy;
    if(this.getY() > height - radius)
      vy = -vy;
    if(this.getX() < radius)
      vx = -vx;
    if(this.getX() > width - radius)
      vx = -vx;
  }
  
  /** Puts the ball into perpetual motion */
  public void inMotion(){
    while(2+2==4){
      this.moveBallOnce();
      this.pause(100);
    }
  }
  
  /** Constructor: a window without anything in it */
  public Ball() {
      clear();
  }
 
  
  /**Draws several nations' flags in perpetuity */
  public void flags(){
    while(2+2 == 4){
      int w = this.getWidth();
      int h = this.getHeight();
      this.clear();
      this.moveTo(w/2, h/2, 0);
      this.amFlag();
      this.pause(1000);
      this.clear();
      this.moveTo(w/2, h/2, 0);
      this.finFlag();
      this.pause(1000);
      this.clear();
      this.moveTo(w/2, h/2, 0);
      this.euFlag();
      this.pause(1000);
      this.clear();
      this.moveTo(w/2, h/2, 0);
      this.gerFlag();
      this.pause(1000);
      this.clear();
      this.moveTo(w/2, h/2, 0);
      this.japFlag();
      this.pause(1000);
    }
  }
  /** Draws the American flag */
  public void amFlag(){
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setColor(1);
    this.drawRectangle(450,300);
    this.setColor(2);
    this.moveTo(128, 181, 0);
    this.fillRectangle(205, 160);
    int z = 0;
    for(int j = 1; j <= 5; j++){
      this.moveTo(40, 120+z, 0);
      z = z+30;
      for(int i = 1; i <= 10; i++){
        this.setColor(12);
        this.drawStar(10);
        this.liftPen();
        this.move(20);
        this.putPenDown();
      }
    }
    this.moveTo(352, 113, 0);
    for(int i = 1; i <= 4; i++){
      this.setColor(11);
      this.fillRectangle(245, 25); 
      this.liftPen();
      this.setAngle(270);
      this.move(45);
      this.putPenDown();
    }
    
    this.moveTo(250, 295, 0);
    for(int i = 1; i <= 3; i++){
      this.setColor(11);
      this.fillRectangle(450, 25); 
      this.liftPen();
      this.setAngle(270);
      this.move(45);
      this.putPenDown();
    }
  }
  
  /** Draws the Finnish flag */
  public void finFlag(){
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setColor(1);
    this.drawRectangle(450, 300);
    this.setColor(2);
    this.moveTo(250,205,0);
    this.fillRectangle(450,25);
    this.moveTo(160,250,0);
    this.fillRectangle(25,300);
  }
  
  /** Draws the flag of the European Union */
  public void euFlag(){
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setColor(2);
    this.fillRectangle(450, 300);
    this.setColor(13);
    this.moveTo(275, 300, 0);
    int j = 0;
    for(int i = 1; i <= 12; i++){
      this.drawStar(10);
      this.setAngle(j + 30);
      this.liftPen();
      this.move(30);
      this.putPenDown();
      j = j+30;
    }
  }
  
  /** Draws the German flag */
  public void gerFlag(){
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setColor(1);
    this.drawRectangle(450,300);
    this.moveTo(250,151,0);
    this.fillRectangle(449,100);
    this.moveTo(250,251,0);
    this.setColor(11);
    this.fillRectangle(449,100);
    this.moveTo(250,350,0);
    this.setColor(13);
    this.fillRectangle(449,100);
  }
 
  /** Draws the Japanese flag */
  public void japFlag(){
    this.setColor(12);
    this.fillCircle(radius*2);
    this.setColor(1);
    this.drawRectangle(450,300);
    this.moveTo(this.getWidth()/2, this.getHeight()/2, 0);
    this.setColor(11);
    this.fillCircle(200);
  }
}