//Valondine Jeudy //December 10, 2014 //CST 112 // Prof. M String title = "Ball & Buttons Array"; String author= "Valondine Jeudy"; String course = "CST 112"; int tableColor=0; //Declare and define button and arrays. Ball[] jeudy= new Ball[4]; Button[] valondine = new Button[6]; void setup() { background(150, 25, 0); size(700, 600); //Four Balls jeudy[0] = new Ball(255, 0, 0); jeudy[0] = new Ball(155, 100, 255, 0, 0); jeudy[1] = new Ball(0, 255, 0); jeudy[1] = new Ball(254, 255, 0, 255, 0); jeudy[2] = new Ball(0, 0, 255); jeudy[2] = new Ball(width/2, 155, 0, 0, 255); jeudy[3] = new Ball(255, 155, 147); jeudy[3] = new Ball(120, 300, 255, 155, 147); // Six Buttons valondine[0] = new Button(85, height-35, 55, 50, "Red"); valondine[1] = new Button(160, height-35, 55, 50, "Blue"); valondine[2] = new Button(250, height-35, 55, 50, "Green"); valondine[3] = new Button(350, height-35, 55, 50, "Pink"); valondine[4] = new Button(450, height-35, 55, 50, "Table"); valondine[5] = new Button(550, height-35, 55, 50, "Exit"); } void draw() { //draws the next frame background(255, 0, 0); //changes the color of the table. if (tableColor%10==0) { fill(135, 206, 250); drawpoolTable(); } else { fill(225, 20, 147); drawpoolTable(); } // Title, etc. textSize(12); fill(0, 25, 145); text( title, width/2-35, 20 ); text( author, 5, height-10 ); text(course, width-50, 15); } void drawpoolTable() { // draws the pool table with the balls. rectMode(CENTER); rect(width/2, height-300, width-100, height-100); jeudy[0].drawMovement(); jeudy[1].drawMovement(); jeudy[2].drawMovement(); jeudy[3].drawMovement(); valondine[0].drawButton(); valondine[1].drawButton(); valondine[2].drawButton(); valondine[3].drawButton(); valondine[4].drawButton(); valondine[5].drawButton(); } class Ball { // properties float x, y; float dx = 4, dy = 4; float w = 35, h=35; int r, g, b; // Constructors: set colors to the balls Ball(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } //Five argument constructor Ball( float x, float y, int r, int g, int b) { this.x = x; this.y = y; this.r = r; this.g = g; this.b = b; } // methods void drawMovement() { //call the drawBall,moveBall and bounceTable methods drawBall(); moveBall(); bounceTable(); } void drawBall() { // draw the balls and show. fill(r, g, b); ellipse(x, y, w, h); } void moveBall() { //make the ball moves x+= dx; y+= dy; } void bounceTable() { // bounce the ball of the pool table. // x axis if ( x < 60 || x> width-55) { dx*=-1; x+=dx; } // y axis if ( y < 60 || y>height-55 ) { dy*=-1; y+=dy; } } } class Button { // PROPERTIES// float x, y, w, h; String name; // positions and name of buttons. CONSTRUCTOR Button(float x, float y, float w, float h, String name) { this.x = x; this.y = y; this.w = w; this.h = h; this.name = name; } void drawButton() { //template for the buttons textSize(15); fill(0, 155, 155); rectMode(CORNER); rect(x, y, w, h); fill(125, 0, 65); text(name, x+(w/2)-15, y+20); } boolean isHit() { //if buttons are clicked. if (mouseX> x && mouseX< x+w && mouseY> y && mouseY < y+h) { return true; } else { return false; } } } void mousePressed() { //handles mouse if (valondine[0].isHit()) { redReset(); } if (valondine[1].isHit()) { greenReset(); } if (valondine[2].isHit()) { blueReset(); } if(valondine[3].isHit()){ pinkReset(); } if (valondine[4].isHit()) { tableColor+=5; } if (valondine[5].isHit()){ exit(); } } void keyPressed() { //handles keys if (key == '1') { redReset(); } if (key == '2') { greenReset(); } if (key == '3') { blueReset(); } } void redReset() { //change position of red ball jeudy[0].x = 75; jeudy[0].y = 150; } void greenReset() { // change position of green ball jeudy[2].x = 80; jeudy[2].y = 250; } void pinkReset(){ jeudy[3].x = 87; jeudy[3].y = 375; } void blueReset() { // change position of blue ball jeudy[1].x = 95; jeudy[1].y = 320; }