//Project 8//
//Kornprapa Kost//

String author=  "Kornprapa Kost";
Ball redKost, greenKost, blueKost;
Button redButton, greenButton, blueButton, tableButton;

int table= 0;

void setup(){
  size(600,400);
  
  redKost = new Ball(255,0,0);
  greenKost = new Ball(0,255,0);
  blueKost = new Ball(0,0,255);
  redButton = new Button(100, 370, 50, 25, "RED");
  greenButton = new Button(200, 370, 50, 25, "GREEN");
  blueButton = new Button(350, 370, 50, 25, "BLUE");
  tableButton = new Button(450, 370, 50, 25, "TABLE");
  initialize();
}

void initialize(){
  redKost.randomize();
  greenKost.randomize();
  blueKost.randomize();  
}

void draw(){
   background(0);
   table();
   moveBalls();
   drawBalls();
   drawButtons();
   fill(255);
   text( author, width/2-50, 25 );
}

void table(){
 
 if(table%2==0){
    fill(0,140,200);
  }
 else{
    fill(200,200,0);
  }
  stroke(200,30,60); // pink wood rail//
  strokeWeight(25); 
  rectMode(CORNER);
  rect(50, 50, 500, 300, 15, 15, 15, 15); //table size at 500*300//
}
  
void moveBalls(){
  //// Move each ball, based on its velocity (dx,dy),
  redKost.move();
  greenKost.move();
  blueKost.move();

}
void drawBalls() {
   //// Draw each ball////
   redKost.show();
   greenKost.show();
   blueKost.show();
}

void drawButtons(){
  redButton.display1();
  greenButton.display2();
  blueButton.display3();
  tableButton.display();
}

 void mousePressed(){
  if(redButton.mouseHit()){
    redReset();
  }
  if(greenButton.mouseHit()){
    greenReset();
  }
  if(blueButton.mouseHit()){
    blueReset();
  }
 if(tableButton.mouseHit()){
  table = table+1;
  }
 }
 
void keyPressed(){
  if(key == 'q') exit();
  if(key == '1')redReset();
  if(key == '2')greenReset();
  if(key == '3')blueReset();
 }
 
 void redReset(){
  redKost.x = 100;
  redKost.y = 150;
}

void greenReset(){
  greenKost.x = 100;
  greenKost.y = 150;
}

void blueReset(){
  blueKost.x = 100;
  blueKost.y = 150;
 }


                 ///////Ball Class///////
class Ball{
  color c;
  float x=0,y=0;    // Position of this ball
  float dx=4,dy=4;  

  //// CONSTRUCTORS ////
  Ball( float x0, float y0 ) {
    x=  x0;
    y=  y0;
  }
  
  Ball( int r, int g, int b ) {
    c=  color( r, g, b );
  }

  //// METHODS  ////
void move() {
    //// Move ball by (dx,dy)
    x=  x + dx;
    y=  y + dy;
    // Bounce off walls.
    if (x < 75)  {  dx=  -dx;    x=  x + dx;  }
    if (x > 525) {  dx=  -dx;    x=  x + dx;   }
    if (y < 75)  {  dy=  -dy;    y=  y + dy;   }
    if (y > 325) {  dy=  -dy;    y=  y + dy;  }
  }

void show() {
    //// Draw ball at position x,y ////
    fill(c);
    noStroke();
    ellipseMode(CENTER);
    ellipse(x, y, 25, 25);  // Diameter 25//
  }
  
void randomize() {
    //// Set random values for x, y, dx, dy.
    x=  100 + random(100);
    y=  100 + random(100);
    dx=  1 + random(3);
    dy=  1 + random(3);
  }
}//End Ball Class//

               ///////Button Class//////////
class Button{
float x;
float y;
float w;
float h;
String text;

              //////Constructors/////////
Button (float x, float y, float w, float h, String text){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.text = text;
  }
  
              ///////METHODS/////////
void display(){
    fill(255);
    noStroke();
    rect(x,y,w,h);
    fill(0);
    textSize(13);
    text(text,x+h/4,y+w/3);
  }
 
 void display1(){
    fill(255,0,0);
    noStroke();
    rect(x,y,w,h);
    fill(255);
    textSize(13);
    text(text,x+h/4,y+w/3);
  }
   
   void display2(){
    fill(0,255,0);
    noStroke();
    rect(x,y,w,h);
    fill(255);
    textSize(13);
    text(text,x+h/4,y+w/3);
  }
  
  void display3(){
    fill(0,0,255);
    noStroke();
    rect(x,y,w,h);
    fill(255);
    textSize(13);
    text(text,x+h/4,y+w/3);
  }
  
  
   boolean mouseHit(){
   if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h)
     {
      return true;
     }
    else {
     return false;
    } 
  }//boolean//
}//Button Class//

