This week we'll see how threads can be used to animate a couple of traffic signals.
Download the starter jar file, StopLight.jar, and save it into the directory csci/201.
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
StopLightFrame is a subclass of
Two instances of this class are created by the
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
StopLightFrame to obtain a
StopLightControl object which will
be used to control the bulbs of a traffic signal.
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
are controlled by the associated
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,
It also begins by importing
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.
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
It then begins excuting the code in its
run method of
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.
NormalControl class so that its
traffic signal behaves like a real traffic
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.
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
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.