Threads and Animation

This week we'll see how threads can be used to animate a couple of traffic signals.

To Get Started ...

Download the starter jar file, StopLight.jar, and save it into the directory csci/201.

Unjar the archive file (Help), create a new NetBeans project called StopLight, and mount the StopLight directory created when you unjarred the archive file (Help).

Now compile the code and run it. One signal does nothing. The other should cycle through all its lights. You will need to wait for at least one complete cycle before this gets at all interesting.

Start by looking in the file StopLight.java. Notice that it begins by importing two classes edu.unca.cs.csci201.LabAids.StopLightFrame and edu.unca.cs.csci201.LabAids.StopLightControl.

The class StopLightFrame is a subclass of the Java Frame class. Two instances of this class are created by the main method of StopLight. That's why you see two traffic signals on your screen.

Look a bit more at main and you'll see that it invokes the getControl method of StopLightFrame to obtain a StopLightControl object which will be used to control the bulbs of a traffic signal. Note that main also sets the titles and initial location of the traffic signals. But its most exiting act is creating and then starting a thread. After that it leaves all the hard work to its thread and to any event handlers associated with the Java frames.

We mentioned that the bulbs of a StopLightFrame object are controlled by the associated StopLightControl object. This is done with twelve unimaginably named methods for the purpose. We trust you'll be able to figure out what each of these methods accomplish. You should have also noted by now that it is possible for more than one light to be on. Real traffic signals aren't supposed to do this.

Take a look at our other class, ControlNormal. It also begins by importing StopLightControl. ControlNormal extends Thread. So far you've probably thought of your Java code as being executing one statement at a time. However, its is possible for a Java application to have several threads. Each thread will be executing its own Java statements. Thus it is possible for several Java statements to be concurrently executing within your program. You'll have to wait until a later course to see how this is done with only one microprocessor chip. For now, just trust us.

When a class extends Thread it should overide the method run. Generally, the thread is created and started by another class. (Remember main.) It then begins excuting the code in its run method.

The run method of ControlNormal is very long. It enters an infinite loop where it cycles through all the lights, turning them on and off. Notice that it sleeps 1000 milliseconds, or one second, between each of its control actions.

Instructor Checkoff ...

Modify the NormalControl class so that its traffic signal behaves like a real traffic signal. It might take a bit of experimentation to get the right speed to your transitions. In case you don't remember, the yellow light comes on after the green and you never have green in both directions at the same time. (At least that's what the judge told me at my last visit to traffic court.)

Show your instructor the normal traffic light.

Instructor Checkoff ...

Now you need to make the frame labeled Flash display lights that flash red in the North-South direction and yellow in the East-West direction. You'll need to create a new Thread class, similar to ControlNormal and associate it with the Flash display to accomplish this task.

Show your instructor the flashing traffic light. If you'd rather do something more flashy, that's ok with us.