|
Previous Next
Part C - Wake Up - Add Alarm Beep
In this step, we will add the sound needed to wake someone when the desired time is reached.
The alarm is set by entering a value for Wake Up Time then pressing the Start button. After pressing the Start button, the same button is labelled Stop. Then pressing the Stop button will turn off the alarm notification process. To deal with pressing the button, we will need to implement the ActionListener.
import java.awt.event.*;
public class WakeUp extends JApplet implements ActionListener {
btnStartStop.addActionListener(this);
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStartStop)) {
if (btnStartStop.getText().equals("Start")) {
btnStartStop.setText("Stop");
txtStatusInfo.setText("Alarm process started");
start();
} else if (btnStartStop.getText().equals("Stop")) {
btnStartStop.setText("Start");
txtStatusInfo.setText("Alarm process has been stopped.");
stop();
}
}
}
We have to enable our applet to play the sound in txtSound by adding the following code. Be sure that the sound file in txtSound is in the directory with which you are working.
import java.applet.AudioClip;
AudioClip alarmSound;
alarmSound = getAudioClip(getCodeBase(), txtSound.getText());
We need to make our applet runnable and add a thread for checking if it's time to sound the alarm. We will start the thread in the applet's start method and stop the thread in the applet's stop method. We will add the following code. Since the applet is using the Runnable interface, we will have to implement the run method before we can compile, but we will discuss the run method shortly.
public class WakeUp extends JApplet implements Runnable, ActionListener {
Thread thrSound;
public void start() {
if (thrSound == null) {
thrSound = new Thread(this);
thrSound.start();
}
}
public void stop() {
if (thrSound != null) {
if (alarmSound != null)
alarmSound.stop();
thrSound = null;
}
}
In the start method of our applet, we want to set up the time for the alarm. This time is located on the screen in field txtWakeUpTime. We will convert this into a Calendar format and place it in field ckDate. If the time has passed, we will add a day to it so that the alarm is set for the next day.
int ckHour;
int ckMin;
Calendar ckDate;
String s1 = txtWakeUpTime.getText();
ckHour = Integer.parseInt(s1.substring(0, 2));
ckMin = Integer.parseInt(s1.substring(3, 5));
ckDate = new GregorianCalendar();
Calendar cal = new GregorianCalendar();
int mm = cal.get(Calendar.MONTH);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int yy = cal.get(Calendar.YEAR);
ckDate.set(yy, mm, dd, ckHour, ckMin);
if (ckDate.compareTo(cal) < 0) ckDate.add(Calendar.DATE, 1);
We only want to check the time if the Start button has been pressed. We will set a switch for this called doCheck. In the run method, we will check the time and decide to run the sound. The "while(true)" cause an infinite loop because the time must be compared continuously. To share the time with other processes, we will put this applet asleep every second.
boolean doCheck = true;
public void stop() {
doCheck = false;
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
GregorianCalendar currtime = new GregorianCalendar();
if (doCheck)
if (ckDate.compareTo(currtime) < 0) {
runTheSound();
}
}
}
public void runTheSound() {
doCheck = false;
if (alarmSound != null)
alarmSound.loop();
}
public void actionPerformed(ActionEvent evt) {
if (btnStartStop.getText().equals("Start")) {
doCheck = true;
The code we have thus far follows.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.AudioClip;
public class WakeUp extends JApplet implements Runnable, ActionListener {
DteTimePanel dtTimePane = new DteTimePanel();
JLabel lblWakeUpTime = new JLabel("Wake Up Time");
JTextField txtWakeUpTime = new JTextField("20:33", 5);
JTextField txtSound = new JTextField("train.wav");
JTextField txtSrchRadio =
new JTextField("http://www.radio-locator.com/");
JComboBox cboxPickRadio = new JComboBox();
JButton btnStartStop = new JButton("Start");
JButton btnSrchRadio = new JButton("Find");
JButton btnPlayRadio = new JButton("Play");
JTextField txtStatusInfo = new JTextField("Status information");
AudioClip alarmSound;
Thread thrSound;
int ckHour;
int ckMin;
Calendar ckDate;
boolean doCheck = false;
public void init() {
alarmSound = getAudioClip(getCodeBase(), txtSound.getText());
JPanel panOuter = new JPanel();
GridLayout gridlay1 = new GridLayout(3, 1);
panOuter.setLayout(gridlay1);
panOuter.add(dtTimePane);
JPanel panWakeUpTime = new JPanel();
FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT);
panWakeUpTime.setLayout(flow1);
panWakeUpTime.add(lblWakeUpTime);
panWakeUpTime.add(txtWakeUpTime);
JPanel panButtons = new JPanel();
GridLayout gridlay2 = new GridLayout(1, 3);
panButtons.setLayout(gridlay2);
panButtons.add(btnSrchRadio);
panButtons.add(btnPlayRadio);
btnStartStop.addActionListener(this);
panButtons.add(btnStartStop);
JPanel panSetAlarmTime = new JPanel();
GridLayout gridlay3 = new GridLayout(2, 2);
panSetAlarmTime.setLayout(gridlay3);
panSetAlarmTime.add(panWakeUpTime);
panSetAlarmTime.add(panButtons);
panSetAlarmTime.add(txtSound);
panSetAlarmTime.add(txtSrchRadio);
panOuter.add(panSetAlarmTime);
cboxPickRadio.addItem("radio station 1");
cboxPickRadio.addItem("radio station 2");
cboxPickRadio.addItem("radio station 3");
cboxPickRadio.setEditable(true);
JPanel panBottom = new JPanel();
GridLayout gridlay4 = new GridLayout(2, 1);
panBottom.setLayout(gridlay4);
panBottom.add(cboxPickRadio);
panBottom.add(txtStatusInfo);
panOuter.add(panBottom);
setContentPane(panOuter);
repaint();
}
public void start() {
String s1 = txtWakeUpTime.getText();
ckHour = Integer.parseInt(s1.substring(0, 2));
ckMin = Integer.parseInt(s1.substring(3, 5));
ckDate = new GregorianCalendar();
Calendar cal = new GregorianCalendar();
int mm = cal.get(Calendar.MONTH);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int yy = cal.get(Calendar.YEAR);
ckDate.set(yy, mm, dd, ckHour, ckMin, 00);
if (ckDate.compareTo(cal) < 0) ckDate.add(Calendar.DATE, 1);
if (thrSound == null) {
thrSound = new Thread(this);
thrSound.start();
}
}
public void stop() {
doCheck = false;
if (thrSound != null) {
if (alarmSound != null)
alarmSound.stop();
thrSound = null;
}
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
GregorianCalendar currtime = new GregorianCalendar();
if (doCheck)
if (ckDate.compareTo(currtime) < 0) {
runTheSound();
}
}
}
public void runTheSound() {
doCheck = false;
if (alarmSound != null)
alarmSound.loop();
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStartStop)) {
if (btnStartStop.getText().equals("Start")) {
btnStartStop.setText("Stop");
txtStatusInfo.setText("Alarm process started");
doCheck = true;
start();
} else if (btnStartStop.getText().equals("Stop")) {
btnStartStop.setText("Start");
txtStatusInfo.setText("Alarm process has been stopped.");
stop();
}
}
}
}
class DteTimePanel extends JPanel {
String lastTime = "";
public void paint(Graphics screen) {
GregorianCalendar xday = new GregorianCalendar();
String xtime = xday.getTime().toString();
Color fgColor = new Color(40, 120, 160);
Font type = new Font("Monospaced", Font.BOLD, 20);
screen.setFont(type);
screen.setColor(getBackground());
screen.drawString(lastTime, 5, 15);
screen.setColor(fgColor);
screen.drawString(xtime, 5, 15);
lastTime = xtime;
repaint();
}
}
Previous Next
|