Java Reference
In-Depth Information
g.drawLine(burstX, burstY, endX, endY);
}
}
currentStep++;
}
// This firework is done, so the panel
// class can let it be garbage collected.
public boolean isDone() {
return currentStep >= steps;
}
}
By the way, you can use similar code to draw a bullet going out and exploding against a target. Ideas
are often similar to other ideas, so the same code can often be used for a different purpose with little
modification, and this is one of those ideas. I encourage you to create that animation. Try it both with all
12 splines (the lines that radiate from the center) and with just the splines that are not over the target
(which might be more realistic, depending on what kind of event you choose to model.)
Sprite Animation
So far, we concentrated on drawing simple objects made of circles and lines. You can use the same
technique to create more complex objects, but it's often easier to use a series of images. Those images
are called sprites . Many animations rely on sprites to create complex images that take a lot of code (and
thus time) to create programmatically. Also, using images lets companies hire artists to create the
images and programmers to create the animation. Consequently, the quality of both the images and the
code improves, making a better program. .Separation of concerns is also a good principle for business,
where it's generally called specialization.
In this case, we create a simple four-image sprite that represents an expanding bullseye. Figure 10-4
shows all four sprite images together and zoomed in ten times.
Figure 10-4. The sprites used by the MouseSprites program
I zoom it in ten times to reveal something about how things are drawn on a monitor (or TV, your
phone, or anything with a digital display)—there are no curves. Any curve you see on a screen is a
collection of straight lines (each called a segment of the curve). The more segments, the better the
illusion, but there is no curve. Create a circle on the screen (the drawOval method in the Graphics class
Search WWH ::




Custom Search