The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Creating a GUI with JFC/Swing
Lesson: 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(outside of the tutorial), to see whether you can use one of them instead of writing your own program.

Creating an Animation Loop with Timer

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 both use a Timer object to implement the animation loop. You can use these examples as templates for your own animation.

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.

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.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search