Modify a text file: $/balls2.java
$/balls2.java
//// Demo of Ball objects [BAM:2A18f] //// // Several ball objects, moving around on the screen. // Object-oriented programming (without arrays). Ball one, two, three; Ball four, five, six, seven, eight; float helpX=10, helpY=10, helpLine=0; Ball[] all= new Ball[8]; void setup() { size(800, 600); smooth(); begin(); } void begin() { //// Create objects. Initialize, as necessary. one= new Ball( color(255, 0, 0), 1, "1" ); two= new Ball( color(0, 255, 0), 2, "2" ); three= new Ball( color(0, 0, 255), 3, "3" ); four= new Ball( color(255, 255, 0), 4, "4" ); five= new Ball( color(0, 255, 255), 5, "5" ); six= new Ball( color(255, 0, 255), 6, "6" ); seven= new Ball( color(255, 127, 0), 7, "8" ); eight= new Ball( color(0, 0, 0), 8, "8" ); eight.fillNum= color(255); //// Fill array with all balls. all[0]=one; all[1]=two; all[2]=three; all[3]=four; all[4]=five; all[5]=six; all[6]=seven; all[7]=eight; } void draw() { //// Draw the next frame. background( 200, 200, 255 ); ellipseMode(RADIUS); if (key == '?') help(); moveAll(); collisions(); showAll(); } void moveAll() { //// Move all three balls. one.move(); two.move(); three.move(); // More: four.move(); five.move(); six.move(); seven.move(); eight.move(); } void collisions() { //// Check for collisions; exchange momentum. /* one.bounce( two ); one.bounce( three ); two.bounce( three ); //// one.bounce(four); one.bounce(five); one.bounce(six); one.bounce(seven); one.bounce(eight); two.bounce(four); two.bounce(five); two.bounce(six); two.bounce(seven); two.bounce(eight); three.bounce(four); one.bounce(five); three.bounce(six); three.bounce(seven); three.bounce(eight); four.bounce(five); four.bounce(six); four.bounce(seven); four.bounce(eight); five.bounce(six); five.bounce(seven); five.bounce(eight); six.bounce(seven); six.bounce(eight); seven.bounce(eight); */ int n= all.length; for (int j=0; j
width-2*radius) { dx = -dx; x += 3*dx; } if (y<2*radius || y>height-2*radius) { dy = -dy; y += 3*dy; } // Move x += dx; y += dy; } public void speedup() { //// Increase speed of this ball. dx += 1; dy += 1; } public void stop(float x, float y) { //// Stop if (x,y) is near this ball. if ( dist(x, y, this.x, this.y ) < radius ) { dx=0; dy=0; } } public void bounce( Ball other ) { //// Bounce if other (x,y) is near this ball. if ( dist( other.x, other.y, this.x, this.y ) < radius ) { float t; t=other.dx; other.dx=this.dx; this.dx=t; t=other.dy; other.dy=this.dy; this.dy=t; // Bounce away from each other. this.x += this.radius*this.dx; this.y += this.radius*this.dy; other.x += other.radius*other.dx; other.y += other.radius*other.dy; } } public void show() { //// Show the ball. fill(c); if (key == '=') whereami(); ellipse( x, y, radius, radius ); fill(fillNum); text( num, x-5, y+5 ); // Show # on ball } void whereami() { //// Display positions. float xx= (int) 2*radius; // Offsets for text float yy= 0; text( x, x+xx, y+12*yy++ ); text( y, x+xx, y+12*yy++ ); text( c, x+xx, y+12*yy++ ); // Also show dx yy=0; xx= (int) 2*radius; // Offsets for text text( dx, x+xx+100, y+12*yy++ ); text( dy, x+xx+100, y+12*yy++ ); } }// END of class Ball //