CHAPTER 10 ■ ANIMATION
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
// This method adds a new expanding bullseye.
@Override
public void mouseReleased(MouseEvent e) {
mouseSpritePanel.add(e.getX(), e.getY());
}
}
As you can see, this class is similar to the Fireworks class, with the addition of implementing the
MouseListener interface (and all its required methods). We made the MouseListener implementation (at
the top of the class) and the MouseListener methods (at the bottom of the class) bold, to make them easy
to find. Like the Fireworks class, the MouseSprites class has a timer to control how often to draw a new
sprite, and it has a panel class to provide a place for drawing the sprites.
■ Note You must create a directory called C:\test\sprites (on Windows) or C:/test/sprites (on Unix or Linux) and
put the sprite images in that directory to get the program to show the sprites. Alternatively, you can put the images
elsewhere and change the code to point to that directory.
A more complex program might expect the location of the sprite files as an argument to the
program. It's generally considered bad form to hard-code paths and file names into a program, but I
don’t want to introduce the additional complexity of reading them from an argument (I want to focus on
animation instead). Adding the location as an argument would be a good exercise, if you want to expand
this simple program a bit. If you do, remember that the argument would be handled in the main method
of the MouseSprites class, not in the MouseSpritePanel class.Listing 10-7 shows the MouseSpritePanel
class.
Listing 10-7. The MouseSpritePanel class
package com.bryantcs.examples.animation;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;