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.
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"> ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.