Makes a ball that bounces as if under the influence of gravity.

Solution

public class GravityBall extends GraphicsProgram {

    private static final int SIZE = 20;
    private static final int DELAY = 20;
    
    private static final double GRAVITY = 0.5;
    private static final double DAMPING = 0.7;
	
	public void run() {
		// Draw a ball on the top right corner
		GOval oval = new GOval(0,0,SIZE, SIZE);
		oval.setFilled(true);
		add(oval);
		
		// Declare variables for velocity
		double vx = 3;
		double vy = 0;
		
		while(true) {
			// update vy
			vy = vy + GRAVITY;
			
			// should the ball bounce?
			if(oval.getY() > (getHeight() - SIZE) && vy > 0) {
				vy = vy * -DAMPING;
			}
			
		    // move
			oval.move(vx, vy);
			
			// animation pause.
			pause(DELAY);
		}
	}
}