Your first animation using java!!!
Standard Drawing: Animation and images Images.
/****************************************************************************** * Compilation: javac BouncingBall.java * Execution: java BouncingBall * Dependencies: StdDraw.java * * Implementation of a 2-d bouncing ball in the box from (-1, -1) to (1, 1). * * % java BouncingBall * ******************************************************************************/ public class BouncingBall { public static void main(String[] args) { // set the scale of the coordinate system StdDraw.setXscale(-1.0, 1.0); StdDraw.setYscale(-1.0, 1.0); StdDraw.enableDoubleBuffering(); // initial values double rx = 0.480, ry = 0.860; // position double vx = 0.015, vy = 0.023; // velocity double radius = 0.05; // radius // main animation loop while (true) { // bounce off wall according to law of elastic collision if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx; if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy; // update position rx = rx + vx; ry = ry + vy; // clear the background StdDraw.clear(StdDraw.GRAY); // draw ball on the screen StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx, ry, radius); // copy offscreen buffer to onscreen StdDraw.show(); // pause for 20 ms StdDraw.pause(20); } } } Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne. Last updated: Tue Aug 30 09:58:33 EDT 2016.
Classwork:
Add this program to your current project.
Make the following changes, submit a screenshot for each change and the final program:
- Comment out the “clear” function and describe what you see. Remove the comments to continue working.
- Make the ball color change when it bounces. Choose a random color at every bounce.
- Change the background color from the original.
- Change the shape of the ball.