Modify a text file: $/balls.java
$/balls.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; 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); } void draw() { //// Draw the next frame. background( 200, 200, 255 ); 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); } void showAll() { //// Show all three balls. one.show(); two.show(); three.show(); //// More four.show(); five.show(); six.show(); seven.show(); eight.show(); } //// EVENT HANDLERS //// void keyPressed() { //// Respond to different keys, when pressed. switch (key) { case '0': begin(); break; case '1': one.speedup(); break; case '2': two.speedup(); break; case '3': three.speedup(); break; case '4': four.speedup(); break; case '5': five.speedup(); break; case '6': six.speedup(); break; case '7': seven.speedup(); break; case '8': eight.speedup(); break; case 's': frameRate( 2 ); break; case 'S': frameRate( 30 ); break; } } void help() { //// Explain keys, etc. helpX= width * 2 / 3; helpY= 100; next( "Click any ball to stop it." ); next( "Number key (1,2,3) speeds up that ball" ); next( "0 key restarts (with balls at random poisitions)" ); } void next(String s ) { //// Show help text on next line. text( s, helpX, helpY ); helpY += 12; // Move down 12 pixels, for next line. } void mousePressed() { //// Click on ball to stop it. one.stop( mouseX, mouseY ); two.stop( mouseX, mouseY ); three.stop( mouseX, mouseY ); } //// CLASS DEFINITIONS //// // A "ball" that moves, bounces, draws itself, etc. class Ball { float x=0, y=0, dx=3, dy=2; // Coordiantes, velocity float radius=30; color c= color(255); color fillNum= color(0); int num=0; String s="0"; //// CONSTRUCTOR(S) //// Ball( color c, int num, String s ) { //// Construtor to set color, number, and name. Random x,y & speed this.c= c; this.num= num; this.s= s; // float r= radius; x= random( r, width-r ); y= random( r, height-r ); dx= random( 5 ); dy= random( 3 ); } //// METHODS //// public void move() { //// Move ball from x,y by dx,dy // First, check for bounce if (x<2*radius || x>width-2*radius) { dx = -dx; x += 2*dx; } if (y<2*radius || y>height-2*radius) { dy = -dy; y += 2*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 //