import java.awt.*;
/** Assignment A4: using a Turtle. Time spent on assignment = 6 hours */
// Interesting procedure is the static method Ball.fractal(). It creates a pattern using random variables.
// Please see specifications for full details.
public class Ball extends YourTurtle {
  private int radius;
  private int vx;
  private int vy;
  
  /** returns radius of the ball*/
  public int getRadius() {
    return radius;
  }
  
  /** returns horizontal velocity (vx) of the ball*/
  public int getVx() {
    return vx;
  }
  
  /** returns vertical velocity (vy) of the ball*/
  public int getVy() {
    return vy;
  }
  
  /** a constructor Ball(x, y, velx, vely, r, c) that initializes a
   * new Ball folder so that the turtle starts at (x, y), 
   * has speed (velx, vely), has radius r, and is drawn with Color c.*/
  public Ball(int x, int y, int velx, int vely, int r, Color c) {
    vx= velx;
    vy= vely;
    radius= r;
    setColor(c);
    moveTo(x, y, 0);
    fillCircle(2*radius);    
  }
  
  /**Constructor Ball(vx, vy, r) that starts the turtle at the 
   * midpoint of the panel, has speed (vx, vy), has radius r, and is a black ball.*/
  public Ball(int velx, int vely, int r) {
    this(width/2, height/2, velx, vely, r, Color.black);
  }
  
  /** Moves the ball once, as given by its speed (vx, vy). 
   *  The ball will bounce off the walls if it hits them*/
  public void moveBallOnce() {
    Color save= getColor();  // stores the original color in variable save
    setColor(Color.white);
    fillCircle(2*radius);
    setAngle(0);
    moveTo(getX() + vx, getY() + vy, 0);
    setColor(save);
    fillCircle(2*radius);
    if (getY() + radius >= height || getY() - radius <= 0) {
      vy= -vy;
    }
    if (getX() + radius >= width || getX() - radius <= 0) {
      vx= -vx;
    }
  }
  
  /** Puts the ball perpetually in motion using an infinite loop.  
   *  Repetend: (1) pauses for 100 microseconds and then (2) moves the ball once.*/
  public void inMotion() {
    while (0 == 0) {
      pause(100);
      moveBallOnce();
    }
  }
  
  /**Creates an interesting, random pattern using n number of edges where n is an 
   * automatically picked random integer between 100 and 500. The pattern may consist
   * of multiple subpatterns. Each subpattern begins with a turtle in a random position
   * in the window, pointed in a random direction. The angle between each new edge increses
   * by 1 degree and each new edge has the next color in the sequence. 
   * The sequence is as follows: black, blue, cyan, dark gray, gray, green, 
   * light gray, magenta, orange, pink, red, white, yellow
   * The length of the edge added is given by d where d is the number of edges in
   * the current subpattern being created + 1. A new subpattern is created if the
   * turtle creating the current subpattern leaves the viewable area of the JFrame
   * The variable d is reset to 1 when a new subpattern begins. 
   * There is a pause of 10 microseconds between the addition of each new edge.*/
  public static void fractal() {
    Turtle a=new Turtle(((int)(width*java.lang.Math.random())), (int)(height*java.lang.Math.random()), 
                        (int)(361*java.lang.Math.random()));
    a.clear();
    
    int ang= (int)(361*java.lang.Math.random());
    int edges= 100 + (int)(401*java.lang.Math.random());
    int c= 1; // loop counter
    int d= 1; // distance to move turtle in each iteration 
    // (also equal to the number of edges in current subpattern + 1)
    // inv: c-1 is the total number of edges drawn, d-1 is the number of edges in the current subpattern.
    while (c <= edges) {
      if (a.getX() > width || a.getX() < 0 || a.getY() > height || a.getX() < 0) { // executes if turtle is outside
        a.moveTo((int)(width*java.lang.Math.random()), // the viewable area of the Jframe
                 (int)(height*java.lang.Math.random()), 
                 (int)(361*java.lang.Math.random()));
        d= 1;
      }
      a.setColor((d%14)-1); // changes to the next color in the sequence
      a.move(d);
      a.addAngle(ang);
      a.pause(10);
      ang= ang+1;
      d= d+1;
      c= c+1; // progress towards termination of loop
    }
    //post: number of edges drawn is equal to the int 'edges'
  }
}