// For Project 9, create an array of ten Ball objects, // each with different colors, positions, speeds, etc. String title = "Billards (Pool) Table"; String title1 = "Adelaida Sanchez"; int numbuttons = 6 ; int numballs = 10 ; Ball[] sanchez = new Ball [numballs]; // Declare and create the Balls array Button[] adelaida = new Button[numbuttons]; // Declare and create the Button array int x = 65; int x1 = 132; int x2 = 200; int x3 = 275; int x4 = 610; int x5 = 670; int y = 505; int w = 50; int h = 35; void setup() { //// Setup: size(800,600); //initialize Balls for (int b = 0; b < sanchez.length; b++) { sanchez[0] = new Ball(255,0,0); sanchez[1] = new Ball(0,255,0); sanchez[2] = new Ball(0,0,255); sanchez[3] = new Ball(100,20,255); sanchez[4] = new Ball(200,200,255); sanchez[5] = new Ball(0,255,255); sanchez[6] = new Ball(200,200,2); sanchez[7] = new Ball(50,50,100); sanchez[8] = new Ball(100,100,55); sanchez[9] = new Ball(200,0,255); } //initialize Buttons for (int i = 0; i < adelaida.length; i++) { adelaida[0] = new Button("RED",65,510,255,0,0); adelaida[1] = new Button("GREEN",135,510,0,255,0); adelaida[2] = new Button("BLUE",205,510,0,0,255); adelaida[3] = new Button("TABLE",280,510,255,255,255); adelaida[4] = new Button("Reset",615,510,250,100,125); adelaida[5] = new Button("Exit",687,510,255,100,35); } initialize ( ); } void initialize ( ) { // Presentation ball positions 0 sanchez[0].randomize ( ); sanchez[1].randomize ( ); sanchez[2].randomize ( ); sanchez[3].randomize ( ); sanchez[4].randomize ( ); sanchez[5].randomize ( ); sanchez[6].randomize ( ); sanchez[7].randomize ( ); sanchez[8].randomize ( ); sanchez[9].randomize ( ); } void draw() { //Balls. scene(); moveBalls ( ); Balls ( ); //text on screen fill ( 255, 255, 255 ); text ( title, 350, 30 ); fill ( 255, 0, 0 ); text ( title1, 35, 575 ); // Show all the buttons for (int i = 0; i < adelaida.length; i++) { adelaida[i].display(); } } void Balls ( ) { //draw ball red, green, and blue sanchez[0].show_ball ( ); sanchez[1].show_ball ( ); sanchez[2].show_ball ( ); sanchez[3].show_ball ( ); sanchez[4].show_ball ( ); sanchez[5].show_ball ( ); sanchez[6].show_ball ( ); sanchez[7].show_ball ( ); sanchez[8].show_ball ( ); sanchez[9].show_ball ( ); } void scene() { // Draw the table background( 0,100,250 ); fill( 0,0,200); //Table: fill( 0,175,255 ); // Light-blue table. stroke( 0,50,150 ); // Dark Blue. strokeWeight(25); rectMode(CORNER); rect(50,50, 700,500); } void moveBalls ( ) { //Move Balls sanchez[0].move ( ); sanchez[1].move ( ); sanchez[2].move ( ); sanchez[3].move ( ); sanchez[4].move ( ); sanchez[5].move ( ); sanchez[6].move ( ); sanchez[7].move ( ); sanchez[8].move ( ); sanchez[9].move ( ); } class Ball { //Balls color c; float x = 0, y = 0; // Balls Position float dx = 8, dy = 8; // Balls Velocity Ball ( int r, int g, int b ) { c = color ( r, g, b ); } void move ( ) { //Move Balls x = x + dx; y = y + dy; //Bounce Off Walls if ( x < 75 ) { dx = -dx; x = x + dx; } if ( x > 720 ) { dx = -dx; x = x + dx; } if ( y < 75 ) { dy = -dy; y = y + dy; } if ( y > 525 ) { dy = -dy; y = y + dy; } } void show_ball ( ) { //Draw Balls fill ( c ); noStroke ( ); ellipseMode ( CENTER ); ellipse (x, y, 40, 40 ); //large the ball } void randomize ( ) { x = 110 + random ( 110 ); y = 110 + random ( 110 ); dx = 1 + random ( 11 ); dy = 1 + random ( 11 ); } } void mousePressed() { if ( adelaida[0].click(mouseX,mouseY) ) { sanchez[0].randomize(); } if ( adelaida[1].click(mouseX,mouseY) ) { sanchez[1].randomize();} if ( adelaida[2].click(mouseX,mouseY) ) { sanchez[2].randomize(); } if ( adelaida[3].click(mouseX,mouseY) ) { background( 200,100,250 ); } if ( adelaida[4].click(mouseX,mouseY) ) { initialize(); } if ( adelaida[5].click(mouseX,mouseY) ) { exit(); } } class Button { // Button Location and Size String s=""; float x = 166; float y = height/2+208 ; float w = 50; float h = 30; int r,g,b; // Constructor All Variables Button( String s, float x, float y, int r, int g, int b ) { this.s= s; this.x= x; this.y= y; this.r= r; this.g= g; this.b= b; } boolean click(float mx, float my) { if (mx < x-w) return false; if (mx > x+w) return false; if (my < y-h) return false; if (my > y+h) return false; return true; } void display() { // Draw the Bottom stroke(0); strokeWeight(2); fill(r,g,b); rectMode(CORNER); // The color changes based on the state of the Button if ( click(mouseX, mouseY)) { fill(0,0,0); } rect(x,y,w,h); text( s, x-w/2+30, y+45 ); } }