RotatingBall.java
Below is the syntax highlighted version of RotatingBall.java
from §2.4 Input and Output.
/*************************************************************************
* Compilation: javac RotatingBall.java
* Execution: java RotatingBall
* Dependencies: StdDraw.java
*
* Assumes the following files are in the same directory: earth.gif.
*
* Implementation of a 2-d bouncing and rotating ball.
*
* % java RotatingBall
*
*************************************************************************/
import java.awt.Color;
public class RotatingBall {
public static void main(String[] args) {
double rx = 0.4, ry = 0.6; // position
double vx = 0.02, vy = 0.03; // velocity
double spin = 20; // spin this many degrees
int SIZE = 512; // size of window
StdDraw.create(SIZE, SIZE);
StdDraw.setScale(0, 0, 1, 1);
while(true) {
StdDraw.clear(Color.black);
// detect collision with wall and reverse velocity
if ((rx + vx > 1) || (rx + vx < 0.0)) { vx = -vx; }
if ((ry + vy > 1) || (ry + vy < 0.0)) { vy = -vy; }
// update position
rx = rx + vx;
ry = ry + vy;
StdDraw.go(rx, ry);
StdDraw.rotate(spin);
StdDraw.spot("earth.gif");
StdDraw.pause(50);
}
}
}
Last updated: Sun Oct 24 22:26:04 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.