CHAPTER 10 ■ ANIMATION
object-oriented programming, encapsulation, and embodies a primary goal of nearly all software
systems, separation of concerns (meaning that each different thing a program can do should be handled
by a different part of the program). Those principles are part of the advantage of object-oriented
programming, because they make designing programs and finding errors much easier. We get to finding
errors in the next chapter.
To illustrate how to animate multiple objects at the same time, we create a simple fireworks
program. When the user clicks the GGo button, it draws fireworks on the screen, in sets of four. Figure 10-
3 shows the Fireworks program in action.
Figure 10-3. The Fireworks program in action
By the way, even with a simple animation program such as Fireworks, a screenshot does not do it
justice. It's the nature of animation that no static image of part of the animation can compare to the
actual animation. So please run the programs in this chapter to see how they really look. I hope you've
been running the programs in the book all along, but, if not, you need to do so in this chapter if you want
to have a real sense of what they do. I suggest you then customize the programs to create your own
versions and ultimately your own original animations.
The Fireworks class works similarly to the ScootBall class, except for one important difference: the
Fireworks class uses a Timer object to control the animation. The Timer class provides instances an easy
way to trigger a process (not just animations, but any process) every so often. Because the timer is
independent of the drawing process, we get much closer to a frame rate of exactly 25 frames per second
(once every 40 milliseconds). Also, the timer simplifies both this class and the drawing panel class,
because we no longer have to manage threads. The program still uses threads, but the Timer class does
that work for us. For those reasons, using a Timer object is much better than making your own thread
and setting it to sleep for some amount of time. (I showed you the other way so that you'd know it exists,
but use Timer objects for your own animations.).Listing 10-3 shows the Fireworks class.