O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Timer Applet

Example 16-3 is a full-featured applet named Countdown . As illustrated in Figure 16-3, it displays an amount of remaining time in minutes and seconds and counts backward down to zero. When it reaches zero, it can play an optional sound and also optionally make the web browser load a new page. The applet demonstrates all of the applet methods listed at the beginning of this chapter, including getParameter( ), getImage( ), getAudioClip( ), showStatus( ), and AppletContext.showDocument( ). It also includes event-handling methods to pause the countdown while the mouse is over the timer. Finally, it demonstrates how to write a Swing-based applet by extending JApplet (you may notice an increase in applet start-up time with the switch to Swing and its attendant overhead). Instead of using a Thread as the Clock applet did, this Countdown applet uses the javax.swing.Timer class.

The Countdown applet

Figure 16-3. The Countdown applet

An important feature of this applet is its configurability: it uses getParameter( ) to read configuration parameters specified in the <param> tags inside the <applet> tag. Invoke the applet with HTML code like this:

<applet code="je3/applet/Countdown.class" codebase="../../" width=400 height=100> <param name="minutes" value="5"> <!-- Count down from 05:00 --> <param name="font" value="helvetica-bold-48"> <!-- Use a big bold font --> <param name="foreground" value="#0000ff"> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required