I have started to work on a break clock in Java to learn and boost my skills in Java. Currently this application is complete. It does not have the function of it's predesessor where you can select the audio file due to Java's short commings in audio compatability. This application is free for other s to adjust and use to thier own purposes since I created it as open source. Next version will be C++ to get this application where it should have full audio capability.
CODE
/**
* Break Clock (Java Version)
*
* @author Michael Camardo
* @version 1
* @date 06/2008
*/
import javax.swing.*;
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.Calendar;
public class BreakClock extends JFrame
{
// instance variables
JLabel lblCurrentTime;
JTextField txtStartTime;
JTextField txtBreak1Time;
JTextField txtLunchTime;
JTextField txtBreak2Time;
JTextField txtLeaveTime;
String hour, min, sec, AMPM;
AudioClip ac;
URL url;
Thread b1 = new BreakTimeClock();
Thread l = new LunchTimeClock();
Thread b2 = new BreakTimeClock();
private ExitButtonHandler ebHandler;
public static void main(String[] args)
{
new BreakClock();
}
/**
* Constructor for objects of class StrategoApp
*/
public BreakClock()
{
// Sets the properties of the app window
this.setSize(250,225);
this.setTitle("Break Clock");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// Sets the layout of the panel to align left
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(new JLabel("The Current Time is: "));
lblCurrentTime = new JLabel("");
panel.add(lblCurrentTime);
panel.add(new JLabel("Start Time: "));
txtStartTime = new JTextField(8);
panel.add(txtStartTime);
panel.add(new JLabel("First Break Time: "));
txtBreak1Time = new JTextField(8);
panel.add(txtBreak1Time);
panel.add(new JLabel("Lunch Time: "));
txtLunchTime = new JTextField(8);
panel.add(txtLunchTime);
panel.add(new JLabel("Second Break Time: "));
txtBreak2Time = new JTextField(8);
panel.add(txtBreak2Time);
panel.add(new JLabel("Leave Time: "));
txtLeaveTime = new JTextField(8);
panel.add(txtLeaveTime);
JButton cmdQuit = new JButton("Quit");
ebHandler = new ExitButtonHandler();
cmdQuit.addActionListener(ebHandler);
panel.add(cmdQuit);
try
{
url = new URL("file:ding.wav"); //include the ding.wav file from windows/media/ folder
ac = Applet.newAudioClip(url);
}
catch
{
}
this.add(panel);
this.setVisible(true);
Timer t = new Timer(1000, new Tick(lblCurrentTime));
t.start();
}
public class Tick implements ActionListener
{
JLabel label;
Calendar time;
Tick(JLabel lblCurrentTime)
{
time = Calendar.getInstance();
label = lblCurrentTime;
}
public void actionPerformed(ActionEvent event)
{
time.setTimeInMillis(System.currentTimeMillis());
int Hour = time.get(Calendar.HOUR_OF_DAY);
if (Hour > 12)
{
Hour = Hour - 12;
AMPM = "PM";
}
else
{
AMPM = "AM";
}
String hour = Integer.toString(Hour);
if (Hour <= 9)
{
// Setting the hour to a formatted string "00 - 09"
hour = hour.trim();
hour = "0" + hour;
}
int Min = time.get(Calendar.MINUTE);
String min = Integer.toString(Min);
if (Min <= 9)
{
// Setting the minute to a formatted string "00 - 09"
min = min.trim();
min = "0" + min;
}
int Sec = time.get(Calendar.SECOND);
String sec = Integer.toString(Sec);
if (Sec <= 9)
{
// Setting the second to a formatted string "00 - 09"
sec = sec.trim();
sec = "0" + sec;
}
// Updates the clock display
lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
// Stores the current time as string
String Time = hour + ":" + min + ":" + sec + " " + AMPM;
if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
{
JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
ac.play();
}
if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
{
JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
b1.start();
ac.play();
}
if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
{
JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);
l.start();
ac.play();
}
if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
{
JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
b2.start();
ac.play();
}
if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
{
JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);
ac.play();
}
}
}
public class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class BreakTimeClock extends Thread
{
public void run()
{
for (int t = 0; t<= 1; t++)
{
try
{
Thread.sleep(900000);
ac.play();
JOptionPane.showMessageDialog(null, "Time to go back to work", "Break Over", JOptionPane.INFORMATION_MESSAGE);
b1.stop();
b2.stop();
}
catch(InterruptedException e)
{
}
)
}
}
public class LunchTimeClock extends Thread
{
public void run()
{
for (int t = 0; t<= 1; t++)
{
try
{
Thread.sleep(1800000);
ac.play();
JOptionPane.showMessageDialog(null, "Time to go back to work", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
l.stop();
}
catch(InterruptedException e)
{
}
}
}
}