CHAPTER 10 ■ ANIMATION
Listing 10-4. The FireworksPanel class
package com.bryantcs.examples.animation;
import java.awt.Graphics;
import javax.swing.JPanel;
public class FireworksPanel extends JPanel{
private static final long serialVersionUID = 1L;
private Firework[] fireworks = new Firework[4];
FireworksPanel() {
init();
}
void init() {
for (int i = 0; i < 4; i++) {
fireworks[i] = new Firework((int)(Math.random()
* this.getWidth()), this.getHeight(), 30);
}
}
public void reset() {
init();
}
public void paint (Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 4; i++) {
if (fireworks[i].isDone()) {
fireworks[i] = new Firework((int)(Math.random()
* this.getWidth()), this.getHeight(), 30);
}
fireworks[i].draw(g);
}
}
}
All the FireworksPanel class really does is manage the set of objects we want to draw, so let's look at
the class that defines the individual objects. The Firework class uses a random color to draw a line
halfway up the screen and then creates a burst of a dozen evenly spaced lines. Conceptually, it's simple,
but it takes a bit of code to get the math right. Listing 10-5 shows the Firework class.
Listing 10-5. The Firework class
package com.bryantcs.examples.animation;
import java.awt.Color;
import java.awt.Graphics;