Different from previous version
If you compare this class with the class named MyCanvas in the earlier program named VectorAdd05 (see Listing 18 ) , you will see that this version of the class is considerably different from the previous version.
The previous version simply overrode the method named paint to cause the off-screen image to be copied to the canvas. That is a perfectly goodapproach for programs where the image that is displayed on the canvas changes only occasionally. However, that is not a good approach for an animation programwhere the image displayed on the canvas changes frequently.
Calling the update method
Recall that in Java, when we want to display an image on the computer screen, we don't call the overridden paint method directly. Rather, we call the method named repaint .
Among other tasks, the repaint method acts as a traffic cop to decide which application will be allowed to draw on the screen next, but that isn't themain thrust of our interest here. The main thrust of our interest here is that when we call the repaint method, this eventually results in a call to a method named update . By default, the update method paints a white background on the canvas erasing anything that was previously there. Then the update method calls our overridden paint method to draw whatever it is that we want to draw on the canvas.
Can cause undesirable flashing
Normally this is an adequate approach. However, for an animation program, the repetitive painting of a white background on the canvas can cause an undesirablelevel of flashing in the visual output of the program. In this case, there is no need to paint a white background on the canvas before copying the off-screenimage on the canvas because that image will completely fill the canvas with the new image. Therefore, allowing the canvas to be painted white each time theoff-screen image is copied to the canvas is unnecessary and does cause flashing on my computer.
Overriding the update method
Listing 10 overrides the update method to eliminate the painting of a white background on the canvas. In Listing 10 , the update method simply calls the overridden paint method to copy the off-screen image onto the canvas. On my computer, this results in a much more pleasing animation output.
The actionPerformed method
The next code fragment that I will explain is the actionPerformed method, which is called in response to each click on the Replot button shown in Figure 4 and Figure 5 .
Listing 11 shows an abbreviated listing of the actionPerformed method.
Listing 11 . Abbreviated listing of actionPerformed method. |
---|
public void |
Most of the code in this method is the same as code that I explained in conjunction with Listing 8 , so I won't repeat that explanation here.
(I deleted that code for brevity in Listing 11 . You can view the method in its entirety in Listing 20 .)
The code in Listing 11 picks up with a statement that sets the value of an instance variable named animation to true. This variable is used simply to prevent the animation from starting until the first time the user clicks the Replot button.