float tableColor;

//JG
Ball redJustin, yellowJustin, blueJustin;


void setup() {
  size(500, 400);
  redJustin= new Ball(255, 0, 0);
  yellowJustin= new Ball(255, 243, 0);
  blueJustin= new Ball(0, 0, 255);
  begin();
}

void begin() { // randomizes all ball movement
  redJustin.diffrence();
  yellowJustin.diffrence();
  blueJustin.diffrence();
}

void draw() {
  background(203, 152, 68); // white floor
  drawTable();
  drawBalls();
  moveBalls();
  button(25, 150, 20, 20);
}

void drawTable() {

  fill(tableColor); // grey 
  stroke(100, 50, 50); //table borders 
  strokeWeight(25);
  rect(50, 50, 375, 300); //table
}

void drawBalls() {
  redJustin.show();
  yellowJustin.show();
  blueJustin.show();
}

void moveBalls() {
  redJustin.move();
  yellowJustin.move();
  blueJustin.move();
}






class Ball {
  color c;
  float x=150, y=300;    // Position of this ball.
  float dx=4, dy=4;  // Velocity (pixels per frame).

  // constructors 

  Ball( float x0, float y0 ) {
    x= x0;
    y= y0;
  }
  Ball( int r, int g, int b ) {
    //colors
    c=  color( r, g, b );
  }

  Ball ( ) {
  }
  //// METHODS  ////

  void move() {
    //// Move ball and keep on table
    x=  x + dx;
    y=  y + dy;
    //keeps ball on table
    if (x < width-425) {  
      dx=  -dx;    
      x=  x + dx;
    }
    if (x > width-100) {  
      dx=  -dx;    
      x=  x + dx;
    }
    if (y < 75) {  
      dy=  -dy;    
      y=  y + dy;
    }
    if (y > 325) {  
      dy=  -dy;    
      y=  y + dy;
    }
  }

  void show() {
    //// Draw at (x,y)
    fill(c);
    noStroke();
    ellipseMode(CENTER);
    ellipse(x, y, 25, 25);        // Diameter is 25.
  }

  void diffrence() {
    // Set random values for x, y, dx, dy.
    x=  100 + random(200);
    y=  100 + random(150);
    dx=  1 + random(3);
    dy=  1 + random(2);
  }
}
// interactives 

void button( float x, float y, float w, float h) {

  fill(0);
  rect(25, 150, 20, 20);
  if (mousePressed) {
    if (mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h) {
      println("table color changed");
      fill(0);
      tableColor = 255;
    }
  }
}



void keyPressed() {
  if ( key == '1') {
    redJustin.x = 80;
    redJustin.y = 150;
  }


  if ( key == '2') {
    yellowJustin.x = 80;
    yellowJustin.y = 100;
  }


  if ( key == '3') {
    blueJustin.x = 80;
    blueJustin.y = 150;
  }
}


