[ Home || Sun Java Docs || AlarmClock || ShakeyMessage | Message || Tetris | Piece | Score | Board || Documentation ]


Session 3: Timers, Threads, and GUIs

Swing is a collection of graphical user interface (GUI) components that runs uniformly on any native platform which supports the JVM. Because they are written entirely in Java, these components typically provide functionality above and beyond that provided by the older java.awt package. The abstract window toolkit (awt) relies on native-platform equivalents and so its functionality is limited to the lowest common denominator of all native platforms.

In this session we will be making use the following classes from the indicated packages. Consult the Sun documentation on these classes as needed.

We will perform three experiments, each of which should take about 20 minutes.


Getting Started

Download the six Java source files, which are linked on the navigation bar at the top of this page, to your CFS directory.


Alarm Clock

Download the starter version of this class to your folder and read the documentation. Try the following experiments. The desired output is shown in the transcript below. However two of the methods are not implemented properly, so you'll get different results. Try to figure out how to define the two incorrect methods properly. You may have to consult the Java documentation for the javax.swing.Timer class and the documentation for AlarmClock (the link is at the top of this page.)

> AlarmClock a = new AlarmClock("time for class!");
> a.isSet()
false
> a.getMessage()
"time for class!"
> a.goOff()
time for class!
> a.getDelay()
0
> a.setDelay(2000);
> a.getDelay()
2000
> a.set();              /* the alarm will go off in two seconds */
> a.isSet()
true
time for class!
> a.isSet()
false
> AlarmClock.SNOOZE_DELAY
5000
> a.snooze();           /* the alarm will go off in five seconds */
> a.getDelay()
5000
time for class!
> a.isSet()
false
> AlarmClock.REPEAT_DELAY
300
> a.setRepeats(true);   /* causes the alarm to go off repeatedly
                           at 300ms intervals */
> a.setDelay(0);
> a.setMessage("beep");
> a.set();              /* the alarm will immediately since delay is zero */
beep
beep
beep
beep
beep
beep
beep
beep
beep
> a.snooze()            /* alarm stops, then goes off again after 5 seconds */
beep
beep
beep
beep
beep
beep
> a.turnOff()


Shakey Message

Experiment with the Message class to see how it works.

Run the main method in the ShakeyMessage class like this;

> java ShakeyMessage

Notice that the button is unresponsive.

A ShakeyMessage is a custom JComponent, that is placed on a JFrame by main. A JComponent is-a Container, which means we can add other GUI components to it. In the constructor, we create and add one component, a JButton. Since the layout manager of a JComponent is null, by default, we'll explicitly set the layout to an instance of FlowLayout, which means that the manager will take care of sizing and positioning the button appropriately.

There are two interesting aspects to this program: how to animate the message string and how to make the button reactive to a press.


Tetris

We will now bring the concepts we've learned together in one program to play the game of Tetris.

Fortunately, the basic set-up is the same as in the previous program. We create a custom JComponent and place it on a JFrame.

You can run Tetris like this:

> java Tetris

Read the accompanying documentation and experiment with the individual classes in DrJava.


All materials copyright ©2003 Suzanne Menzel. Last updated October 31, 2003.