CSCI 201 Lab 14 -- Threads and Animation

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

Getting ready

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

Extract the archive file, (see Downloading a Java archive from Lab 2) and open the project in your Lab14 directory. 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.

A tour of the code

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 that you met in last week's lab. Two instances of this class are created by the main method of StopLight. That's why you see two traffic signals on your screen.

Remember the main

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 it's 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.

Turning on the lightbuld

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 traffice signals are supposed to exhibit this property.

The controlling thread of our saga

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 as being executing one Java 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 start 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 sleep 1000 milliseconds, or one second, between each of its control actions.

Your first task

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.)

Normal lab Check-off

Show your instructor the normal traffic light.

Your second task

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.

Flashing lab Check-off

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

Goodbye

It's the last CSCI 201 lab of the semester, but hopefully we'll see you in a CSCI 202 lab soon. Please take a little time to tell your instructor (or send mail to Dean Brock <brock@cs.unca.edu> or Rebecca Bruce <bruce@cs.unca.edu>) your most and least favorite labs from this term.