Threads and Animation

You may have wondered how all those Java applications with animation are programmed. This week we'll take a brief looking at animation by designing a couple of traffic signals.

The thread

So far you've probably thought of your Java code as being executed one statement at a time. However, its is possible for a Java application to have several threads, each executing its own sequence of 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 computer chip; but for now you can enjoy the results.

You need to write the code that uses several threads to program an animation. Typically, each thread will perform an action, such as drawing an image to the display and then sleep a few milliseconds before displaying a new image.

Downloading the project framework

Download, a ZIP file containing a NetBeans project named StopLight and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
Projects panel

Now compile the code and run it. One traffic signal does nothing. The other "tests" its lights by sequentially turning each light off and on.

Making more realistic lights

The main application

Start by looking in the file Notice that it begins by importing three classes edu.unca.cs.csci201.labaids.StopLightPanel, edu.unca.cs.csci201.labaids.StopLightControl, and edu.unca.cs.labaids.FrameMaker.

The edu.unca.cs.csci201.labaids.StopLightPanel class creates those stoplight displays. The StopLightPanel must be placed within a Java frame before it is displayed. This is done by the Component2Frame method.

    StopLightPanel normalPanel = new StopLightPanel() ;
    JFrame normalFrame = FrameMaker.Component2Frame(normalPanel, "Normal") ;

Later the getControl method of StopLightPanel is used to extract an object from the StopLightControl class. This control method, assigned to normalControl, will be used to turn on and off the lightbulbs in the StopLightPanel object assigned to normalPanel.

StopLightControl normalControl = normalPanel.getControl() ;

Finally with a single line, we pass the StopLightControl object normalControl to the constructor for the class ControlNormal and we start a new thread which executes methods of normalCode and thus manipulates the lightbulbs of the normalPanel object.

(new ControlNormal(normalControl)).start() ;

The lightbulb control object

Now let's look at some of these other classes. The bulbs of a StopLightPanel object are controlled by the associated StopLightControl object. This is done with six unimaginably named methods. We trust you'll be able to figure out what each of these methods accomplish. (Hint: Turn on a bulb with true and turn off a bulb with false.) By they way it is possible for more than one light to be on. Real traffic signals aren't supposed to do this.

The threaded control

The real action in this application is not in StopLight. It's within the control classes where the six bulb methods are called and the lightbulbs are turned on and off.

Remember that StopLight creates a ControlNormal object by passing a reference to normalControl, a stoplight control object, to the ControlNormal constructor. The ControlNormal object will retain a reference to this normalControl object and use it turn light bulbs off and on.

Let's take a look at which implements this control class. imports the StopLightControl class it will use to modify the light bulbs. Note that ControlNormal extends the Thread class. Whenever a Java program invokes the start method of a class that extends the Thread class, a new Java thread is created and starts executing its run method as a separate program.

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.

The usual traffic signal

Modify the ControlNormal 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.

The flashing traffic signal

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.