Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 3
When you use double buffering, you need to override the
update() method, since you don't want the applet's background to be cleared before you paint the frame. (You clear the background
yourself by drawing to the offscreen image.)
Now we'll rewrite the
paintFrame() method with a method that animates some images. This adds some minor complications to the problem. Images are rather large
and they are loaded incrementally. It can take a long time for images to be fully drawn, especially when you are loading them
over a slow connection. This is the reason why the
drawImage() method takes a fourth argument, an ImageObserver object. The image observer is an object that is notified when more of the
image data has arrived. To get the images we use the
This first image-animating applet, Example7Applet, uses the following two images:
The world image is used as the background, and the car image is drawn on top of it twice, creating an animation of two cars racing across the world.
Example8Applet shows how to create an animation using separate images for each frame. Here are the 10 frames that are being used:
T1.gif: T2.gif: T3.gif: T4.gif: T5.gif:
T6.gif: T7.gif: T8.gif: T9.gif: T10.gif:
We are still using double buffering to eliminate flashing. The reason is that each image that we are rendering is partially transparent, and we therefore need to erase each frame before drawing the next. This would cause flashing without double buffering.
When displaying sequences of images, you have to be careful to align the images correctly. The easiest way is to make sure that the images are all the same size and can be drawn at the same position. If that isn't the case, your applet will have to draw each frame at a different offset.
When a Java program loads an image, it can display the image before the image is completely loaded. The user sees the image being rendered first incompletely, and then incrementally more and more completely as the image is loaded. This incremental display gives the user feedback (improving perceived performance) and lets the program easily perform other tasks while the image is loading.
Where animation is concerned, incremental image display can be useful for background images, but it can be very distracting when used for the animated images. It is therefore sometimes desirable to wait until the entire animation is loaded before displaying it.
You can use Jim Graham's
MediaTracker class to track the downloading of images, delaying the animation display until the entire set of images is fully downloaded.
Example9Applet shows how to use the
MediaTracker class to download images for the waving Duke animation.