The Algorithm Animation project is a collection of several independent programs. Each algorithm is its own fully functional program. There are 4 main pieces to each algorithm program:
The control panel
The algorithm class and its supporting classes
The animation class
The program begins when the user opens an algorithm web page. The web page then calls the appropriate applet. The user sees some text on the web page detailing which algorithm the user is viewing and some instructions on how to manipulate the algorithm. Part of the web page and the applet in its entirety are shown below.
The Quick Start button will be discussed last. The first text field asks the user how many numbers he or she would like to insert or sort. The user enters this number into the green text field and continues. This number will be called Size. The user may then press one of the three buttons, Random, Sorted, or Reverse. The Random button generates Size random numbers. The Sorted button generates Size sorted numbers starting with 1. The Reverse button generates Size reversely sorted numbers ending with 1. The generated numbers are then displayed in the text area. If the user has entered an illegal Size (not an integer or not in the correct range), a warning is printed in the green text field.
The user may choose to edit the numbers generated by one of the three buttons. The user may also choose not to use the buttons, but enter his or her own input into the text area. When Start Animation is pressed, it calls a method in the main applet, startAnimation(). This method checks that the number of inputs matches Size. If it does not match, a warning is printed to the text area. It also checks that the numbers in the text area are all integers in the correct range separated by spaces. If any of these conditions fails, a warning is printed to the text area. The user must correct the problem as described by the warning in order to continue. If the input is correct, then an algorithm object, and a control panel object are created. The initial speed is read from the control panel and the applet sets the algorithm speed accordingly. The algorithm thread is started and then immediately suspended. The applet also creates a frame on which the animation is draw. The applet implements Runnable. In its run method it sleeps and then repaints. The paint method of the applet paints an image (manipulated by the animation class) to the drawing panel on the frame.
The Quick Start button sets Size to 10 and writes 10 to the appropriate text field. It then simulates a button press of Random. It then simulates a button press of Start Animation. In other words: pressing Quick Start is like entering 10 for size, pressing Random, and pressing StartAnimation.
The Control Panel
The control panel controls the functioning of the algorithm by making calls to a reference of the applet object. The applet object in turn makes calls to the appropriate methods of the algorithm object. The Start results in a call to [algorithmObject].start(). The Suspend button results in a call to [algorithmObject].suspend(). The Step button starts the algorithm object and sets a boolean 'step' in the algorithm to true. The algorithm object checks 'step' at certain points in the program. If 'step' is true, then the algorithm suspends itself. The scrollbar and text field affect the speed of the animation. The algorithm and animation objects have several sleep(delay) calls where delay is an integer. When the speed value is changed in the control panel, the delay time is changed in the algorithm and animation objects. The exit button disposes of the drawing frame and the control panel frame. It stops the algorithm thread. The Restart button effectively performs an Exit press and then a Start Animation press on the applet. Closing the control panel frame serves as an Exit press.
The Algorithm and its Supporting Classes
Each algorithm class extends Thread and the major operations occur in the run() method so that they may be suspended and started easily. The algorithm class and its supporting classes perform the algorithm. When a new algorithm object is instantiated, it creates an animation object. At each step of the algorithm, a call to the animation object is made to animate the step.
The animation class is what actually draws on the image that the applet paints.