// Project 9 by Hugo Bonilla //CST-112 float x = 50; float y = 50; float w = 700; float h = 400; //Balls Ball redBonilla, greenBonilla, blueBonilla; //Buttons Button rButton; Button [] hugo; void setup() { size(800,500); /// 3 balls redBonilla= new Ball(255,0,0); blueBonilla= new Ball(0,0,255); greenBonilla= new Ball(0,255,0); redBonilla.rballs(); blueBonilla.rballs(); greenBonilla.rballs(); ///buttons using array hugo = new Button[5]; // Initialize // hugo[1] = new Button("Red",5,50,50,50); hugo[1].r= 255; hugo[1].g=0; hugo[1].b=0; hugo[2] = new Button("green",5,150,50,50); hugo[2].r=0; hugo[2].g=255; hugo[2].b=0; hugo[3] = new Button("blue",5,250,50,50); hugo[3].r=0; hugo[3].g=0; hugo[3].b=255; hugo[4] = new Button("table",5,350,50,50); hugo[4].r=89; hugo[4].g=209; hugo[4].b=178; //button without array /* rButton= new Button(5,50,50,50); rButton.r=255; rButton.g=0; rButton.b=0; */ } void draw(){ background(167,108,34); msgs(); pooltable(); moveBalls(); drawBalls(); showButtons(); //rButton.show(); } void msgs(){ //title fill(0); textSize(20); text( " Project 8" , width/2 - 50, 30); //author text(" Hugo Bonilla", 20, height -20); } void drawBalls(){ //draws balls on screen redBonilla.show(); greenBonilla.show(); blueBonilla.show(); } void moveBalls(){ redBonilla.move(); blueBonilla.move(); greenBonilla.move(); } void showButtons(){ hugo[1].show(); hugo[2].show(); hugo[3].show(); hugo[4].show(); } void pooltable(){ //border stroke(9,48,129); strokeWeight(25); //table fill(60,159,219); rect(x,y,w,h); } void mousePressed() { // buttons make balls move random & change table color if (hugo[1].isHit(mouseX,mouseY)){ redBonilla.rballs(); } } class Ball { // billiard ball color c; float x= 75, y= 75; // ball position float dx= 5,dy= 5; // ball velocity /////CONSTRUCTORS///// Ball ( int r, int g, int b) { // set colors RGB/ red,reen,blue c= color (r ,g ,b); } Ball () { } ///METHODS/// void move(){ // moves ball.. changes dx & dy x= x + dx; y= y + dy; // MAKE BALLS COUNCE OFF WALLS if (x < 75) { dx = -dx; x = x+ dx;} if (x > 725){ dx = -dx; x = x+ dx;} if (y < 75) { dy = -dy; y = y+ dy;} if (y > 425){ dy = -dy; y = y+ dy;} } void show (){ /// draw ball fill(c); noStroke(); ellipseMode(CENTER); ellipse(x, y, 25,30); } void rballs(){ //rballs= Random Balls x= 75 + random (75); y= 75 + random (75); dx= 5 + random (5); dy= 5 + random (5); } } class Button { //Button to change balls and table int r=255, b= 255, g= 235; float x=5; float y=50; float w=50; float h=50; //button txt String t= "click"; Button ( String txt, float x, float y, float w, float h){ this.x= x; this.y= y; this.w= w; this.h= h; this.t= txt; } // METHODS void show(){ // shows button fill(r,g,b); stroke(0); strokeWeight(1); rect(x,y,w,h); if (isHit(x,y)){ fill(233,233,233); textSize(13); text( t, x+10, y+25); } else { fill(0); textSize(13); text( t, x+10, y+25); } } boolean isHit( float x, float y){ //return "true: if (x ,y) is within this butons shape return(mouseX > x && mouseX < w+x && mouseY > y && mouseY < h+y); } }