This program creates a firework when the user clicks. After some number of clicks a finale consisting of a lot of fireworks is created. After that, mouse clicks do nothing.
import objectdraw.*;
import java.awt.*;
/**
* This program creates a firework on each mouse click. After a fixed number
* of mouse clicks, a finale containing many fireworks is displayed.
*
* @author Barbara Lerner
*/
public class CountingFireworkController extends FrameWindowController
{
// The number of regular fireworks
private static final int MAX_FIREWORKS = 10;
// The number of fireworks in the finale
private static final int NUM_IN_FINALE = 25;
// How many fireworks have been produced so far
private int numFireworks = 0;
// Used to generate a random color for the firework. The red & green components are
// bright while the blue component is dark. This is done to make it more likely that
// the color will stand out against the sky background.
private RandomIntGenerator brightColorGenerator = new RandomIntGenerator (100, 255);
private RandomIntGenerator darkColorGenerator = new RandomIntGenerator (0, 150);
// The random number generator used to determine the size of the firework.
private RandomIntGenerator sizeGenerator = new RandomIntGenerator (25, 75);
// Remembers if the finale has occurred. We only want there to be 1 finale.
private boolean finaleStarted = false;
// The random number generators used to determine a location for each firework during the finale.
private RandomIntGenerator xGenerator;
private RandomIntGenerator yGenerator;
/**
* Create the location generators based on the size of the window.
* Draw the sky background.
*/
public void begin() {
xGenerator = new RandomIntGenerator (0, canvas.getWidth());
yGenerator = new RandomIntGenerator (0, canvas.getHeight());
createSky();
}
/**
* On each mouse click create a firework where the user clicks until
* all the regular fireworks are used up. The next click draws the finale.
* After that mouse clicks do nothing.
* @param point where the user clicked.
*/
public void onMouseClick (Location point) {
// Clear the screen to remove the firework
canvas.clear();
// Draw the sky background again since it also got erased by the
// call to clear.
createSky();
// If there are more regular fireworks, shoot a regular firework
// where the user clicked.
if (numFireworks < MAX_FIREWORKS) {
shootOneFirework(point);
numFireworks++;
}
// If we have run out of regular fireworks and the finale has not
// happened yet, do the finale.
else if (!finaleStarted) {
finale();
}
}
/**
* Draw the sky background.
*/
private void createSky() {
new FilledRect (0, 0, canvas.getWidth(), canvas.getHeight(), canvas).setColor (new Color (24, 5, 162));
}
/**
* Display an individual firework of a random color.
* @param point the center of the firework
*/
private void shootOneFirework(Location point) {
Color nextColor = new Color (brightColorGenerator.nextValue(),
brightColorGenerator.nextValue(),
darkColorGenerator.nextValue());
new Firework (point, nextColor, sizeGenerator.nextValue(), canvas);
}
/**
* Display the finale.
*/
private void finale() {
// Set finaleStarted to true so that later mouse clicks will not display another
// finale.
finaleStarted = true;
// Count how many fireworks have been created in the finale.
for (int finaleCount = 0; finaleCount < NUM_IN_FINALE; finaleCount++) {
// Draw the next firework at a random location.
shootOneFirework(new Location (xGenerator.nextValue(), yGenerator.nextValue()));
}
}
}
import java.awt.*;
import objectdraw.*;
/**
* Draws a pretty firework on the display.
*
* @author Barbara Lerner
*/
public class Firework
{
/**
* Creates and displays a firework
* @param point the center of the firework
* @param color the color the firework should be
* @param radius the size of the firework
* @param canvas the canvas to draw on
*/
public Firework(Location point, Color color, int radius, DrawingCanvas canvas)
{
// angle is the angle of the line from the center point. The first line
// is horizontal
double angle = 0;
// Repeat until we have swept through a circle
while (angle < 2 * Math.PI) {
// Draw a line starting at the center. Calculate where the other endpoint
// should be based on the angle and length of line desired.
new Line (point.getX(), point.getY(),
point.getX() + radius * Math.cos (angle),
point.getY() + radius * Math.sin (angle),
canvas).setColor (color);
// Increase the angle so the next line will be angled differently
// and also so the loop will eventually exit.
angle = angle + Math.PI / 8;
}
}
}