In this discussion, we consider Java multimedia resources. These resources fall into several categories:
- Loading images
- Double buffering
- Loading sound clips
- Loading video
Loading images includes methods for loading and displaying a single image or a series of images to provide animation. Double buffering is used to reduce flickering in image display, caused by the time required to draw the image piecemeal on the screen. To compensate for this delay, the image is constructed in its entirety off-screen and then displayed as a whole. Loading sound and video clips is similar to loading images.
In this discussion, we will consider double buffering with respect to both generated and loaded images.
Double Buffering
The discussion will include five components. First, we look at the underlying concept on which double buffering is based. Next, we look at the basic Java program structure for building double buffering applications. Third, a simple example program is presented, built using ideas from the two earlier discussion. Fourth, a more extensive example is given that makes extensive use of
Threads
. the discussion concludes with a program that loads both audio and image data from external, stored sources.Concept
The basic idea behind double buffering is discussed. One declares a variable which will be used either to load an external image into or to draw on. In either case, the operations are done "off screen." A separate thread is started in which the image is displayed on the screen after the data in the off-screen variable are complete.
Program structure
An outline of a basic program structure for double buffering images is given. It follows the strategy outlined in the previous section.
Example program 1
This example program illustrates double buffering, built using the program structure described in the previous section. It draws 10 randomly placed lines of randomly selected colors on the image at a time; in a separate thread, it then displays the "updated" image.
This program illustrates double buffering using up to 10 separate threads to draw on the Image. It allows the user to add and delete balls, which move around the display. Additional buttons start and stop the animation and quit the program.
This program illustrates double buffering and multiple threads used to load and play an audio clip and a sequence of images that comprise a simple animation.
References
References useful for this discussion include the following:
- Sun's Java Development Kit (JDK)
- Sun's Java API
- Java API Class Hierarchy
- Source Code for JDK Packages