Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Working with Graphics

Performing Animation

What all forms of animation have in common is that they create some kind of perceived motion by showing successive frames at a relatively high speed. Computer animation usually shows 10-20 frames per second. By comparison, traditional hand-drawn animation uses anywhere from 8 frames per second (for poor quality animation) to 12 frames per second (for standard animation) to 24 frames per second (for short bursts of smooth, realistic motion). The next few pages tell you everything you need to know to write a Java program that performs animation.

Before you start: Check out existing animation tools and applets, such as Animator, to see whether you can use one of them instead of writing your own program.

Creating the Animation Loop

The most important step in creating a program that animates is to set up the framework correctly. Except for animation performed only in direct response to external events (such as the user dragging an onscreen object), a program that performs animation needs an animation loop.

The animation loop is responsible for keeping track of the current frame and for requesting periodic screen updates. For applets and many applications, you need a separate thread to run the animation loop. This section contains an example applet and an example application that you can use as templates for all animation.

Generating Graphics

This section features an example that animates primitive graphics.

Eliminating Flashing

The example featured in the previous section is imperfect because it flashes distractingly. This section tells you how to use two techniques to eliminate flashing:

Moving an Image Across the Screen

The simplest form of image animation involves moving an unchanging image across the screen. In the traditional animation world, this is known as cutout animation, since it's generally accomplished by cutting a shape out of paper and moving the shape in front of the camera. In computer programs, this technique is often used for drag and drop interfaces.

Displaying a Sequence of Images

This section tells you how to perform classic, cartoon-style animation, given a sequence of images.

Improving the Appearance and Performance of Image Animation

This section tells you how to use the MediaTracker so that you can delay displaying an animation until all its images are loaded. You'll also get some hints on improving applet animation performance by combining image files and by using a compression scheme such as Flic.


Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Working with Graphics