Modify a text file: $/project4b.java
$/project4b.java
//// Bouncing ball within a box. String title="Bouncing ball within a box."; String news=""; String last=""; String author="B.A.Martin, CST112, Project 4b"; int counter=0; int score=0; float left=50, top=50, right=550, bottom=350; // table boundaries. float ballR, ballG, ballB; // Ball color. float ballX=left, ballY=top+5, ballDX=3, ballDY=2; float redX=left+30, redY=top+30, redDX=3, redDY=2; float greenX=left+30, greenY=top+30, greenDX=3, greenDY=2; float blueX=left+60, blueY=top+60, blueDX=3, blueDY=2; void setup() { //// Set up frame size, etc. size( 600, 400 ); reset(); } void reset() { ballDX= random(10); ballDY= random(4); ballR= random(255); ballG= random(255); ballB= random(255); counter=0; // Zero out the frame count. redX= random(left,right); redY= random(top,bottom); greenX= random(left,right); greenY= random(top,bottom); blueX= random(left,right); blueY= random(top,bottom); } void draw() { // Draw the next frame. scene(); // new scene (bg + text ) table(); action(); }// draw() // void scene() { background( 255, 220, 220 ); // RED, GREEN, BLUE // Messages. text( news, width/4, 40 ); fill(0); text( title, width/4, 20 ); text( author, 20, height-30 ); text( "Frame number "+counter, width/2, height-20 ); text( "SCORE: "+score, width-100, 20 ); text( last, width-100, 40 ); //// Now draw the table. }// scene() // void table() { //// Rectangular table. fill( 220,230,255 ); stroke(0); strokeWeight(4); rectMode(CORNERS); rect( left, top, right, bottom ); } void action() { //// 0.7: Action! //// mickey(); myball(); others(); collisions(); counter++; } void mickey() { //// Move and draw my ball. fill( 255,127,0); rectMode(CENTER); noStroke(); rect( mouseX, mouseY, 50, 90 ); fill( 255,255,255 ); ellipse( mouseX-12, mouseY-30, 10,10 ); ellipse( mouseX+12, mouseY-30, 10,10 ); fill( 0,0,255 ); ellipse( mouseX-12, mouseY-30, 5,5 ); ellipse( mouseX+12, mouseY-30, 5,5 ); fill(0,0,255); text( "Mickey", mouseX-20, mouseY+20 ); if (score<0) fill(255,0,0); else if (score>100) fill(0,0,255); else fill( 255,127,0); // Low score is invisible (same as bg) text( score, mouseX-20, mouseY+40 ); fill( 0 ); ellipse( mouseX-25, mouseY-50, 25,25 ); ellipse( mouseX+25, mouseY-50, 25,25 ); } void myball() { //// 0.8: Action! //// ballX += ballDX; ballY += ballDY; if (ballX < left || ballX > right) ballDX= -ballDX; if (ballY < top || ballY > bottom) ballDY= -ballDY; fill( ballR, ballG, ballB ); stroke( 255-ballR, 255-ballG, 255-ballB ); // Contrast ellipseMode(CENTER); ellipse( ballX, ballY, 25,25 ); noStroke(); } void others() { //// 3 more balls //// ellipseMode(CENTER); noStroke(); //// Red ball. fill(255,0,0); redX += redDX; redY += redDY; if (redX < left || redX > right) redDX= -redDX; if (redY < top || redY > bottom) redDY= -redDY; ellipse( redX, redY, 15,15 ); //// Green ball. fill(0,150,0); greenX += greenDX; greenY += greenDY; if (greenX < left || greenX > right) greenDX= -greenDX; if (greenY < top || greenY > bottom) greenDY= -greenDY; ellipse( greenX, greenY, 15,15 ); //// Blue ball. fill(0,0,255); blueX += blueDX; blueY += blueDY; if (blueX < left || blueX > right) blueDX= -blueDX; if (blueY < top || blueY > bottom) blueDY= -blueDY; ellipse( blueX, blueY, 15,15 ); } void collisions() { //// Detect collisions. if ( dist( ballX, ballY, mouseX, mouseY ) < 20 ) { score -= 50; last= "BALL HIT MICKEY!!!!"; //// Swap velocities. ballDX= -ballDX; ballDY= -ballDY; ballX += 2*ballDX; // Bounce away! ballY += 2*ballDY; } if ( dist( ballX, ballY, redX, redY ) < 20 ) { score += 10; last= "RED"; //// Swap velocities. float dx=ballDX, dy=ballDY; ballDX= redDX; ballDY= redDY; redDX= dx; redDY= dy; } if ( dist( ballX, ballY, greenX, greenY ) < 20 ) { score += 20; last="GREEN"; //// Swap velocities. float dx=ballDX, dy=ballDY; ballDX= greenDX; ballDY= greenDY; greenDX= dx; greenDY= dy; } if ( dist( ballX, ballY, blueX, blueY ) < 20 ) { score += 30; last="BLUE"; //// Swap velocities. float dx=ballDX, dy=ballDY; ballDX= blueDX; ballDY= blueDY; blueDX= dx; blueDY= dy; } } //// EVENT HANDLERS //// void mousePressed() { if ( mouseX>left && mouseX
top && mouseY