CHAPTER 10 ■ ANIMATION
will do the job), and then take a close look at it (perhaps by making a screen capture and zooming in on
it). You see it's a set of straight lines.
As an aside, I work with a bunch of great digital artists whose favorite quotation is from The Matrix,
when the little girl says, “There is no spoon.” She has realized that the matrix is an illusion, and most
digital artists realize (and revel in the fact) that they are making illusions. If you like to make illusions,
you can have a lot of fun (and a great career) with digital art and animation.
I'm We no artist, so I created a simple set of sprites. I also set the timer to one quarter of a second,
making a relatively slow (a video gamer would probably say it's very slow) animation. If you look closely,
you can probably see the individual images as they are drawn. Most animations have a much higher
frame rate (at least comparable to a movie), but this one makes a good demonstration.
When you run the program, click repeatedly to see how many animations you can get going at once.
Doing so demonstrates that you can have multiple sprite animations going at the same time (and test
your ability to click in a hurry—we were able to get four at a time going). Move the mouse a bit between
clicks, so that you can see each animation separately.
I also added one more feature: triggered animation. Rather than run through an endless loop
(perhaps stopped and started by something like a button click), this animation runs once in response to
an event. In this case, the trigger is a mouse click. I added this technique to show you how to create
animations that respond to your users' commands. For example, you might have an endless loop draw
most of the objects in a game and use triggered animations to draw explosions and other items that
appear because of game events.
One other thing to know about sprites is that the individual images can be stored as separate files or
as a single file (often called a sprite sheet). Figure 10-4 can be a sprite sheet if used at its normal zoom
level. In the case of a single file, the program loads part of the image rather than an entire image. For
example, many games that involve maps store the individual terrain symbols together in a single file.
The Java graphics API provides a method for reading part of an image: getSubImage in the BufferedImage
class.
Listing 10-6 shows a program class for a sprite animation program (called MouseSprites because it's
triggered by a mouse click).
Listing 10-6. The MouseSprites class
package com.bryantcs.examples.animation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Timer;
public class MouseSprites implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private MouseSpritePanel mouseSpritePanel = new MouseSpritePanel();