There are many important features to observe in the file BouncyBall.java
protected static final int BALLSIZE = 30; // Constants to determine ball speeds protected static final int MINSPEED = 2; protected static final int MAXSPEED = 7; protected static final int PAUSETIME = 30;
Those constants are used throughout the program. If you change any of them in this one location near the top, you can be sure that you will consistently change the entire program. For example, change the BALLSIZE to 10 and you will have a program that still works just fine, but has smaller balls. Try it. It's important that we used the constant BALLSIZE because otherwise, ensuring consistent changes would be close to impossible. Look at places where BALLSIZE is used such as
right = left + boundary.getWidth() - BALLSIZE;This makes sure the ball appears to bounce correctly off the right wall. If we had simply put the number "30" to specify the size of the ball, and used "30" throughout the program when we needed to refer to it, we would have a nightmare task changing the ball's size if we wanted to be sure that everything remained consistent.
protected static final int BALLSIZE = 30; // Constants to determine ball speeds protected static final int MINSPEED = 2; protected static final int MAXSPEED = 7; protected static final int PAUSETIME = 30; protected FilledOval ball; // components of speed vector protected double xSpeed, ySpeed; // boundaries of playing area protected double left, right, top, bottom; private RandomIntGenerator speedGen = new RandomIntGenerator(MINSPEED, MAXSPEED); private RandomIntGenerator colorGen = new RandomIntGenerator(0,255); protected DrawingCanvas mycanvas;
protected data is visible in and usable by classes that extend this class. It is our intention to make balls that are "smarter" than this
kind of ball. They will, for example, know how to behave if they encounter
a paddle. We will program the smarter balls, not from scratch, but by
extending this class. We have something that works. We need some extra
functionality. That's what inheritance is all about.
while (true) { ... ... }This is not unusual in animations. You create objects that will run until the user turns off the system! Our BouncyBalls will keep bouncing as long as you let them. An infinite loop is the right construct.
Outside of the infinite loop are a declaration
double lastTime, elapsedTime;These are used to keep track of how much time has elapsed since we last drew this object. We use that to figure out how far to move the object. We need an initialization for lastTime, and that's provided by:
lastTime = System.currentTimeMillis();Look at the Java API documentation to see how this call works to give us the current time in milliseconds.
This next exercise may require tools besides a computer. Maybe a watch with a seconds hand or ...?