FileDocCategorySizeDatePackage
Countdown.javaAPI DocExample7927Sat Jan 24 10:44:38 GMT 2004je3.applet

Countdown

public class Countdown extends JApplet implements MouseListener, ActionListener
An applet that counts down from a specified time. When it reaches 00:00, it optionally plays a sound and optionally moves the browser to a new page. Place the mouse over the applet to pause the count; move it off to resume. This class demonstrates most applet methods and features.

Fields Summary
long
remaining
long
lastUpdate
JLabel
label
Timer
timer
NumberFormat
format
Image
image
AudioClip
sound
static String[]
parameterInfo
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

 updateDisplay(); 
public voiddestroy()

 if (image != null) image.flush(); 
public java.lang.StringgetAppletInfo()

	return "Countdown applet Copyright (c) 2003 by David Flanagan";
    
public java.lang.String[][]getParameterInfo()

 return parameterInfo; 
public voidinit()

	// Figure out how long to count for by reading the "minutes" parameter
	// defined in a <param> tag inside the <applet> tag.  Convert to ms.
	String minutes = getParameter("minutes");
	if (minutes != null) remaining = Integer.parseInt(minutes) * 60000;
	else remaining = 600000; // 10 minutes by default

	// Create a JLabel to display remaining time, and set some properties.
	label = new JLabel();
	label.setHorizontalAlignment(SwingConstants.CENTER); 
	label.setOpaque(true);  // So label draws the background color

	// Read some parameters for this JLabel object
	String font = getParameter("font");
	String foreground = getParameter("foreground");
	String background = getParameter("background");
	String imageURL = getParameter("image");

	// Set label properties based on those parameters
	if (font != null) label.setFont(Font.decode(font));
	if (foreground != null) label.setForeground(Color.decode(foreground));
	if (background != null) label.setBackground(Color.decode(background));
	if (imageURL != null) {
	    // Load the image, and save it so we can release it later
	    image = getImage(getDocumentBase(), imageURL);
	    // Now display the image in the JLabel.
	    label.setIcon(new ImageIcon(image));
	}	

	// Now add the label to the applet. Like JFrame and JDialog, JApplet
	// has a content pane that you add children to
	getContentPane().add(label, BorderLayout.CENTER);

	// Get an optional AudioClip to play when the count expires
	String soundURL = getParameter("sound");
	if (soundURL != null) sound=getAudioClip(getDocumentBase(), soundURL);

	// Obtain a NumberFormat object to convert number of minutes and
	// seconds to strings.  Set it up to produce a leading 0 if necessary
	format = NumberFormat.getNumberInstance();
	format.setMinimumIntegerDigits(2); // pad with 0 if necessary

	// Specify a MouseListener to handle mouse events in the applet.
	// Note that the applet implements this interface itself
	addMouseListener(this);

	// Create a timer to call the actionPerformed() method immediately,
	// and then every 1000 milliseconds. Note we don't start the timer yet.
	timer = new Timer(1000, this);
	timer.setInitialDelay(0);  // First timer is immediate.
    
public voidmouseClicked(java.awt.event.MouseEvent e)

public voidmouseEntered(java.awt.event.MouseEvent e)

	pause();                // pause countdown
	showStatus("Paused");   // display statusline message
    
public voidmouseExited(java.awt.event.MouseEvent e)

	resume();               // resume countdown               
	showStatus("");         // clear statusline
    
public voidmousePressed(java.awt.event.MouseEvent e)

public voidmouseReleased(java.awt.event.MouseEvent e)

voidpause()

	// Subtract elapsed time from the remaining time and stop timing
	long now = System.currentTimeMillis();  
	remaining -= (now - lastUpdate);
	timer.stop();   // Stop the timer
    
voidresume()


    // Start or resume the countdown
      
	// Restore the time we're counting down from and restart the timer.
	lastUpdate = System.currentTimeMillis();
	timer.start();  // Start the timer
    
public voidstart()

 resume(); 
public voidstop()

 pause(); 
voidupdateDisplay()

	long now = System.currentTimeMillis();  // current time in ms
	long elapsed = now - lastUpdate;        // ms elapsed since last update
	remaining -= elapsed;                   // adjust remaining time
	lastUpdate = now;                       // remember this update time

	// Convert remaining milliseconds to mm:ss format and display 
	if (remaining < 0) remaining = 0; 
	int minutes = (int)(remaining/60000);
	int seconds = (int)((remaining%60000)/1000);
	label.setText(format.format(minutes) + ":" + format.format(seconds));

	// If we've completed the countdown beep and display new page
	if (remaining == 0) {
	    // Stop updating now.
	    timer.stop();
	    // If we have an alarm sound clip, play it now.
	    if (sound != null) sound.play();
	    // If there is a newpage URL specified, make the browser
	    // load that page now.
	    String newpage = getParameter("newpage");
	    if (newpage != null) {
		try {
		    URL url = new URL(getDocumentBase(), newpage);
		    getAppletContext().showDocument(url);
		}
		catch(MalformedURLException ex) { showStatus(ex.toString()); }
	    }
	}