// Source code copyright by Developer's Daily, all rights reserved. // http://DevDaily.com import java.awt.*; import java.applet.*; import java.net.*; import symantec.itools.awt.FormattedTextField; public class SiestaApplet extends Applet { public void stop () { // override the applets normal "stop" method tcf.stop(); // explicitly stop any running thread tcf = null; // thread points to nothing } public void start () { // override the applet's start() method resetScreen(); // to properly restart the applet when a user leaves } // our HTML page and then comes back void resetButton_Clicked(Event event) { tcf.stop(); tcf = null; resetScreen(); } public void resetScreen() { tcf = new ThreadedCountdownField(this); sleepTimeTextField.setText(""); sleepTimeTextField.setEditable(true); sleepTimeTextField.requestFocus(); timeRemainingLabel.setText(""); } void stopButton_Clicked(Event event) { tcf.stop(); timeRemainingLabel.setText(""); } void startButton_Clicked(Event event) { if (tcf.isAlive()) { tcf.resume(); } else { long timeToSleep = (new Long(sleepTimeTextField.getText().trim())).longValue(); tcf.setTimeToSleep(timeToSleep); sleepTimeTextField.setEditable(false); tcf.start(); timeRemainingLabel.setText(""); } } void testAlarmButton_Clicked(Event event) { gong.play(); } public void init() { super.init(); //{{INIT_CONTROLS setLayout(null); setSize(505,300); setBackground(java.awt.Color.white); startButton = new java.awt.Button(); startButton.setLabel("Start the Timer"); startButton.setBounds(25,109,120,25); startButton.setFont(new Font("Dialog", Font.PLAIN, 12)); add(startButton); stopButton = new java.awt.Button(); stopButton.setLabel("Stop the Timer"); stopButton.setBounds(25,144,120,25); stopButton.setFont(new Font("Dialog", Font.PLAIN, 12)); add(stopButton); label1 = new java.awt.Label("How long do you want to sleep?"); label1.setBounds(25,24,184,21); label1.setFont(new Font("Dialog", Font.PLAIN, 12)); add(label1); sleepTimeTextField = new symantec.itools.awt.FormattedTextField(); sleepTimeTextField.setMask("999999"); sleepTimeTextField.setBounds(218,24,54,22); sleepTimeTextField.setFont(new Font("Dialog", Font.PLAIN, 12)); add(sleepTimeTextField); sleepTimeTextField.setCursor(new Cursor(Cursor.TEXT_CURSOR)); testAlarmButton = new java.awt.Button(); testAlarmButton.setLabel("Test the Alarm"); testAlarmButton.setBounds(25,245,120,25); testAlarmButton.setFont(new Font("Dialog", Font.PLAIN, 12)); add(testAlarmButton); label2 = new java.awt.Label("(time in seconds)"); label2.setBounds(295,25,100,20); label2.setFont(new Font("Dialog", Font.PLAIN, 12)); label2.setForeground(java.awt.Color.black); add(label2); label3 = new java.awt.Label("(Note: 60 seconds = 1 minute, 3600 seconds = 1 hour)"); label3.setBounds(118,49,264,20); label3.setFont(new Font("Dialog", Font.PLAIN, 12)); label3.setForeground(java.awt.Color.gray); add(label3); label4 = new java.awt.Label("Siesta Time Remaining (seconds):"); label4.setBounds(156,146,169,20); label4.setFont(new Font("Dialog", Font.PLAIN, 12)); label4.setForeground(java.awt.Color.black); add(label4); timeRemainingLabel = new java.awt.Label(""); timeRemainingLabel.setBounds(344,147,104,20); timeRemainingLabel.setFont(new Font("Dialog", Font.BOLD, 12)); timeRemainingLabel.setForeground(java.awt.Color.lightGray); add(timeRemainingLabel); resetButton = new java.awt.Button(); resetButton.setLabel("Reset the Timer"); resetButton.setBounds(25,180,120,25); resetButton.setFont(new Font("Dialog", Font.PLAIN, 12)); add(resetButton); //}} tcf = new ThreadedCountdownField(this); gong = getAudioClip(getDocumentBase(), AUDIO_FILE); } public boolean handleEvent(Event event) { if (event.target == testAlarmButton && event.id == Event.ACTION_EVENT) { testAlarmButton_Clicked(event); return true; } if (event.target == startButton && event.id == Event.ACTION_EVENT) { startButton_Clicked(event); return true; } if (event.target == stopButton && event.id == Event.ACTION_EVENT) { stopButton_Clicked(event); return true; } if (event.target == resetButton && event.id == Event.ACTION_EVENT) { resetButton_Clicked(event); return true; } return super.handleEvent(event); } //{{DECLARE_CONTROLS java.awt.Button startButton; java.awt.Button stopButton; java.awt.Label label1; symantec.itools.awt.FormattedTextField sleepTimeTextField; java.awt.Button testAlarmButton; java.awt.Label label2; java.awt.Label label3; java.awt.Label label4; java.awt.Label timeRemainingLabel; java.awt.Button resetButton; //}} ThreadedCountdownField tcf; AudioClip gong; final String AUDIO_FILE = "audio/gong.au"; }