Data Structures and Algorithms I

Program 2

In this program assignment you will gain experience reading and modifying Java code. Your task is to rewrite the BallWorld applet so it is a Java application. In addition, you will eliminate every usage of inner classes to gain experience implementing callbacks explicitly. A callback occurs when an event handler accesses a method back in the object that instantiated the event handler. This requires explicitly passing a reference to that object to the event handler's constructor so the handler has access to the originating object.

You will also add a control panel and a variety of Ball types to create a fun and exciting Ball World that you can use to amaze your family and friends..

Start by downloading the original applet and verifying that it runs correctly. Examine the code to be sure you understand everything that this applet does and how it works. Pay attention to the inheritance concepts.

After you are sure you understand the existing Applet, begin to modify it to be a standalone application. You must modify BallWorld to extend a JFrame instead of JApplet. Add a main method to BallWorld just for testing. It will instantiate the BallWorld JFrame and size it appropriately. The program should end when the JFrame is closed.

The next step is to systematically remove all inner classes (Ball, BallDisplayer, and the TimerListener (which is an anonymous inner class). Each class must go in a separate file. Attack this one class at a time, and test everything as you go. The constructors for these classes may need to accept a reference to some object so they can access the fields of that object. This may also requre public access methods be added to some classes. You should refer to Chapter 7 for concepts used in this program.

When everything works properly as an application, you will need to add a few things to the project.

You are to create five additional Ball classes (subclasses extending Ball) that move and change in interesting ways. You must use inheritance appropriately!

A Pulsing Ball behaves like a Ball in every way, except that it changes size gradually, shrinking a little each time it moves until it reaches s% of its original size; then it gradually grows back to its original size and the cycle repeats. The shrink factor is specified in the constructor and may be any number up to 100 percent.

A Squishy Ball behaves like a Ball in every way, except that as it moves, it squishes. This is accomplished by modifying the height and width (maintaining the same center location) each time the ball moves. The ball should gradually get wider (and shorter) until its smaller dimension reaches s% of its original radius (s - the squish factor - is specified in the constructor). The ball then gets taller (and less wide) until the same limit is reached. Again, s is limited from 0% to 100%.

A Colorful Ball behaves like a Ball in every way, except that its color gradually changes. This ball must remember its original color and then each time it moves, modify its original color slightly. The modification is accomplished by adding 1 to each of the RGB component values. Of course, when one of these reaches its maximum you can no longer increase them. At this point, the RGB values begin to decrease until they reach the original color. The process then repeats.

You must create 2 additional Ball classes of your choice!

You will need to add a control panel (sample code provided) with textfields where you will enter the number of each type of ball. This will be the main application. It will instantiate and display the BallWorld frame.

You should move the enumerated type from the Control Panel file to its own file and make it public. You must also add your ball types to the enumerated type list of values. An Apply button needs an ActionListener (make another class for this) that will reinitialize the world of balls in the BallWorld so it contains Ball instances according to the numbers in these textfields. The balls should be constructed with random attributes. You will probably need to add one or more methods to BallWorld that can be called from the control panel to replace the Balls in the ArrayList with a new set of balls. These might mimic the existing init method, possibly with a parameter or two to control the number of balls.

There will also be a start/stop button on the control panel to start and stop the ball motion. This button acts like a toggle; it should read Start initially, then change to Stop after clicking. Of course, another click will change it back to Start. And the balls should all start and stop as commanded. Another method will be needed to allow this action.

Remember - no inner classes.

Be sure to document every class and method. Don't forget to check the documentation standards and include the signed cover sheet with your printed submission.