Bouncing Ball Demo

This was an exam question.

Note: this applet uses the Java 1.1 event model and browsers are just now starting to implement this. I should probably put the class files out so people can acually run this from their browsers.

The main limitations are the flickering in the animation and the poor collision test algorithm. It has been localized to Francais but it uses the actionCommand stuff to keep track of the buttons so their names can be changed arbitrarily.

I had no luck drawing on the canvas from code that was not a canvas method. Perhaps the answer here is to completely override the paint method and make it into a no op. On the Macintosh one would take the old ball region, subtract the new ball region, then clear that difference region to the background color. Since Java doesn't implement regions it is not clear what the best animation method would be. Perhaps clearing the old ball rectangle then drawing the new ball rectangle would be a reasonable compromise. It also might be worth doing the ball as an offscreen graphic, since just blitting it over could be faster than Bresenham or whatever the Java ellipse drawer is doing.

The collision detector worked fine in the Macintosh screen saver I wrote ten years ago and took it from. In that program the only velocities were -1, 0, and +1. But in this applet the ball is seen to bounce short of the wall by as much as the absolute value of the velocity. I thought a little about making it better, but that isn't the thrust of this exam question, demonstrating command of AWT is, and I think this submission shows that.

Back to ZBEN's home page.