//15 hours
//our interesting procedure: ballsLineUp()

import javax.swing.*;
import java.awt.*;

public class Ball extends YourTurtle{
  private int radius; //radius of the ball
  private int vx; //speed in horizontal direction
  private int vy; //speed in vertical direction
  private int xx; //position x
  private int yy; //position y
  
  /** = radius of the ball */
  public int getRadius(){
    return radius;
  }
  
  /** = speed in horizontal direction, vx, of the ball */
  public int getVX(){
    return vx;
  }
  
  /** = speed in vertical direction, vy, of the vall */
  public int getVY(){
    return vy;
  }
  
  /** Constructor: ball starts in the middle (at postion (250, 250)),
   *  has speed (vx, vy), has radius r, and is drawn with color black. */
  public Ball(int vx, int vy, int r){
    this(width/2, height/2, vx, vy, r, Color.black);
  }
  
  /** Constructor: ball starts at position (x, y), has speed (vx, vy),
   *  has radius r, and is drawn with Color c. */
  public Ball(int x, int y, int vx, int vy, int r, java.awt.Color c){
    xx= x;
    yy= y;
    this.vx= vx;
    this.vy= vy;
    radius= r;
    java.awt.Color color= c;
    moveTo(x, y, 0);
    setColor(c);
    fillCircle(2*r);
  } 
  
  /** Ball moves once with speed (vx, vy). */
  public void moveBallOnce(){
    Color save= getColor();
    setColor(Color.white);
    fillCircle(2*radius);
    xx= vx+xx;
    yy= vy+yy;
    moveTo (xx,yy,0);
    setColor(save);
    fillCircle(2*radius);
    
    if(yy < radius)
      vy= -vy;
    if(xx < radius)
      vx= -vx;
    if(height-yy < radius)
      vy= -vy;
    if(width-xx < radius)
      vx= -vx;
  }
  
  /** Ball is perpetually in motion. */
  public void inMotion(){
    while(true){
      moveBallOnce();
      pause(100);
    }
  }
  
  /** Different colored balls bounce and line up horizontally. */
  public static void ballsLineUp(){
    Ball a= new Ball(37,37,0,20,35, Color.red);
    Ball b= new Ball(107,37,0,20,35,Color.orange);
    Ball c= new Ball(177, 37, 0, 20, 35, Color.yellow);
    Ball d= new Ball(247, 37, 0, 20, 35, Color.green);
    Ball e= new Ball(317, 37, 0, 20, 35, Color.blue);
    Ball f= new Ball(387, 37, 0, 20, 35, Color.magenta);
    Ball g= new Ball(457, 37, 0, 20, 35, Color.pink);
    
    while(true){
      int k= 0;
      while(k != 25){
        a.moveBallOnce();
        a.pause(100);
        
        k= k+1;
      }
      
      while(k != 50){
        b.moveBallOnce();
        b.pause(100);
        k= k+1;
      }
      
      while(k != 75){
        c.moveBallOnce();
        c.pause(100);
        k= k+1;
      }
      
      while(k != 100){
        d.moveBallOnce();
        d.pause(100);
        k= k+1;
      }
      
      while(k != 125){
        e.moveBallOnce();
        e.pause(100);
        k= k+1;
      }
      
      while(k != 150){
        f.moveBallOnce();
        f.pause(100);
        k= k+1;
      }
      
      while(k != 175){
        g.moveBallOnce();
        g.pause(100);
        k= k+1;
      }
      
    }
  }
}