Modify a text file: $/three.java
$/three.java
//// Demo of Ball objects [BAM:2A18c] //// // Several ball objects, moving around on the screen. // Object-oriented programming (without arrays). Ball one, two, three; 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" ); } void draw() { //// Draw the next frame. background( 200, 200, 255 ); if (key == '?') help(); moveAll(); collisions(); showAll(); text( one.y, 100,100 ); } void moveAll() { //// Move all three balls. one.move(); two.move(); three.move(); } void collisions() { //// Check for collisions; exchange momentum. one.bounce( two ); one.bounce( three ); two.bounce( three ); } void showAll() { //// Show all three balls. one.show(); two.show(); three.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 '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*dx; } // 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; } } 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 //