-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClockWithAudio.java
executable file
·100 lines (83 loc) · 2.83 KB
/
ClockWithAudio.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ClockWithAudio extends JApplet {
protected AudioClip[] hourAudio = new AudioClip[12];
protected AudioClip[] minuteAudio = new AudioClip[60];
// Create audio clips for pronouncing am and pm
protected AudioClip amAudio =
Applet.newAudioClip(this.getClass().getResource("audio/am.au"));
protected AudioClip pmAudio =
Applet.newAudioClip(this.getClass().getResource("audio/pm.au"));
// Create a clock
private StillClock clock = new StillClock();
// Create a timer
private Timer timer = new Timer(1000, new TimerListener());
// Create a label to display time
private JLabel jlblDigitTime = new JLabel("", JLabel.CENTER);
@Override /** Initialize the applet */
public void init() {
// Create audio clips for pronouncing hours
for (int i = 0; i < 12; i++)
hourAudio[i] = Applet.newAudioClip(
this.getClass().getResource("audio/hour" + i + ".au"));
// Create audio clips for pronouncing minutes
for (int i = 0; i < 60; i++)
minuteAudio[i] = Applet.newAudioClip(
this.getClass().getResource("audio/minute" + i + ".au"));
// Add clock and time label to the content pane of the applet
add(clock, BorderLayout.CENTER);
add(jlblDigitTime, BorderLayout.SOUTH);
}
@Override /** Override the applet's start method */
public void start() {
timer.start(); // Resume clock
}
@Override /** Override the applet's stop method */
public void stop() {
timer.stop(); // Suspend clock
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
clock.setCurrentTime();
clock.repaint();
jlblDigitTime.setText(clock.getHour() + ":" + clock.getMinute()
+ ":" + clock.getSecond());
if (clock.getSecond() == 0)
announceTime(clock.getHour(), clock.getMinute());
}
}
/** Announce the current time at every minute */
public void announceTime(int hour, int minute) {
// Announce hour
hourAudio[hour % 12].play();
try {
// Time delay to allow hourAudio play to finish
Thread.sleep(1500);
// Announce minute
minuteAudio[minute].play();
// Time delay to allow minuteAudio play to finish
Thread.sleep(1500);
}
catch(InterruptedException ex) {
}
// Announce am or pm
if (hour < 12)
amAudio.play();
else
pmAudio.play();
}
public static void main(String[] args) {
ClockWithAudio applet = new ClockWithAudio();
applet.init();
applet.start();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("ClockWithAudio");
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setSize(400, 320);
frame.setVisible(true);
}
}