This is a bonus program! It's meant to be a bit challenging, but completing this program will definitely help you write your final project.

Write a GraphicsProgram that displays a timer counting down from ten, once a second, and displays a message once the timer hits zero.

Setup the timer

To implement this timer, we will need both a logical representation and a visual representation of our countdown value, which are given by two class variables:

private GLabel countdownLabel; // display of timer value
private int countdown; // actual timer value

Choose a large font for your timer, and start by displaying the starting time (TOTAL_TIME) on the screen. You can convert integers to strings by adding the following method to your code:

public String formatIntToString(int x) {
    return "" + x;
}
Your starting canvas should look something like this (I used the font Consolas with size 200):

Implement the countdown

Write your animation loop which updates both the logical and visual countdown representations every second.
Do not add new GLabels! Just keep changing the text of the old one using GLabel's method setLabel().

// Sets countdownLabel's text to be "hello world"
countdownLabel.setLabel("hello world");

Remember that delay is counted in milliseconds, and we want the timer to operate by the second.

Formatting your timer

Zero-padding

Since this is a timer, we want to zero-pad our numbers, which means adding a zero to the front of a number so that it stays the same width. How would you update the visual representation of your countdown timer to display the following?

Hint: Make your changes to formatIntToString().

Fixed-width fonts

Depending on your choice of font, your timer might still not align, even after zero-padding. This is something that a lot of graphics-oriented programmers struggle with! Make sure you are picking a fixed-width font, where each character/digit is the same width. Common examples are Consolas, Monaco, and Courier.

Finishing up

Once the timer hits zero, display a message. It can be anything!

Bonus: Add minutes

Now add minutes to your countdown display, so that the timer reads 1:00, 0:59, 0:58, and so on.

General guidelines:

  • Try to keep a single variable that records your remaining time in seconds. Avoid tracking minutes and seconds separately.
  • Separate your single variable into minutes and seconds when you display the counter. Consider defining methods to make your logic very clear.
  • To debug quickly, speed up your timer by reducing the pause time.
  • Remember the "mod" function! Example: 5 % 2 = 1

Bonus bonus: How would you add milliseconds? The display convention is: 1:03.234.