<< Chapter < Page Chapter >> Page >

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.

Questions & Answers

how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask