CSC 160 Lab 6-2 Timer Project: Odometers

In this lab, you will write a GUI program that simulates an automobile odometer. Here are a few screen shots showing the initial frame as first shown when the program begins to run, and also after the program has been running a while.

The user interface consists of a row of text fields at the top of the frame, and below that, two buttons with text "Drive car" and "Stop car". The row of text fields models the digits of an automobile odometer. The relative positions of the user interface components must remain stable even if the frame is resized. Write the program to use a 3-digit odometer: however, the program must be written in such a way that the number of digits in the odometer can be changed by modifying the program at a single point.

The program works as follows. Pressing the Drive car button starts a time which increments the odometer every tenth of a second. Pressing the Stop Car button stops the timer. In this way, a user can start or stop the odometer from running as often as they like. When the odometer overflows the number of digits allocated, it ``turns over" and starts again at 0.

The program is very similar to the color cycling program that we did in class. You should review that program and make sure you understand it. Your program must meet the following requirements:

  1. Have separate listener classes for the timer and for the two buttons.
  2. The program should not use any nested classes.

Start by building the user interface and display to make sure it works. The user interface should consist of a row of text fields, with each text field being one column wide. Use a panel with a FlowLayout to hold an array of text fields.

The heart of this program, other than the user interface, is the listener class for the timer. The listener class should have a constructor that takes an array of text fields as parameter. The listener class should also have an array of integer to correspond to the the array of text field. In the timer listener class constructor, use new to create the array of integers the same size as the array of textfield, and fill all the integers with 0. You should also reflect the array entries onto the textfields.

Remember: The program must be written in such a way that the number of digits in the odometer can be changed in a single place in your program.

Due Date: Monday of Week 7