import objectdraw.*; import java.awt.*; // a textual countdown to zero public class Countdown extends ActiveObject { // the delay between countdown stages private static final int MILLISECONDS_COUNTDOWN_PAUSE = 100; // multiplies stage of countdown to get font size private static final int COUNTDOWN_MULTIPLIER = 5; // font sizse for final exclamation point private static final int EXCLAMATION_POINT_FONT_SIZE = 200; // keeps track of the current countdown stage private int countdownStage; // the countdown text private Text countdownText; // need to have the center that was sent in to the constructor so we // can always make sure the Text object is centered private Location textCenter; // start the countdown with countdownStart, save the Location the Text // should be centered on, and create and center the Text object public Countdown(Location inTextCenter, int countdownStart, DrawingCanvas canvas) { countdownStage = countdownStart; textCenter = inTextCenter; countdownText = new Text(countdownStage, 0, 0, canvas); countdownText.moveTo(textCenter.getX() - countdownText.getWidth()/2, textCenter.getY() - countdownText.getHeight()/2); start(); } // while the countdown has not reached zero, reset the Text object to the next // countdown level, make the font smaller, and recenter public void run() { while (countdownStage >= 0) { countdownText.setText(countdownStage); countdownText.setFontSize(countdownStage * COUNTDOWN_MULTIPLIER); countdownText.moveTo(textCenter.getX() - countdownText.getWidth()/2, textCenter.getY() - countdownText.getHeight()/2); pause(MILLISECONDS_COUNTDOWN_PAUSE); countdownStage--; } countdownText.setText("!"); countdownText.setFontSize(EXCLAMATION_POINT_FONT_SIZE); countdownText.moveTo(textCenter.getX() - countdownText.getWidth()/2, textCenter.getY() - countdownText.getHeight()/2); } }