import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import javax.swing.JFrame;
public class Clock2 extends JFrame implements Runnable {
private NumberCounter hours;
private NumberCounter minutes;
private NumberCounter seconds;
private boolean ampm;
private String time;
private boolean running;
private boolean stopped;
private Graphics g;
public static void main(String[] args) {
Clock2 clock = new Clock2();
clock.SysTime();
clock.showClock();
}
public Clock2() {
super("ClockShow");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
stopClock();
setVisible(false);
dispose();
System.exit(0);
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
int code =evt.getKeyCode();
switch (code) {
case KeyEvent.VK_R : //Start
restartClock();
break;
case KeyEvent.VK_S : //stop
stopClock();
break;
case KeyEvent.VK_X : //destroy
destroyClock();
System.out.println("Clock destroyed");
break;
default :
;
}
}
});
setSize(320, 240);
setVisible(true);
g = this.getContentPane().getGraphics();
hours = new NumberCounter(13);
minutes = new NumberCounter(60);
seconds = new NumberCounter(60);
upDate();
}
public Clock2(int h, int m, int s, String amorpm) {
hours = new NumberCounter(13);
minutes = new NumberCounter(60);
seconds = new NumberCounter(60);
hours.setTime(h);
minutes.setTime(m);
seconds.setTime(s);
if ((amorpm.equals("am")) || (amorpm.equals("pm"))) {
if (amorpm.equals("am"))
ampm = false;
if (amorpm.equals("pm"))
ampm = true;
} else
System.out.println("Geben sie bitte am oder pm ein");
upDate();
}
public void tickSec() {
seconds.count();
if (seconds.getV() == 0) {
minutes.count();
if (minutes.getV() == 0)
tickHour();
}
upDate();
}
public void tickMin() {
minutes.count();
if (minutes.getV() == 0)
tickHour();
upDate();
}
public void tickHour() {
hours.count();
if (hours.getV() == 0) {
hours.setTime(1);
if (ampm == true)
ampm = false;
else
ampm = true;
}
upDate();
}
public String getTime() {
return time;
}
private void setAP(String ap) {
if (ap.equals("am"))
ampm = false;
if (ap.equals("pm"))
ampm = true;
upDate();
}
private String getAP() {
if (ampm == false)
return "am";
else
return "pm";
}
private void upDate() {
time =
hours.getValue()
+ ":"
+ minutes.getValue()
+ ":"
+ seconds.getValue()
+ " "
+ getAP();
}
public void SysTime() {
Calendar cal = Calendar.getInstance();
hours.setTime(cal.get(Calendar.HOUR_OF_DAY));
minutes.setTime(cal.get(Calendar.MINUTE));
seconds.setTime(cal.get(Calendar.SECOND));
}
public void showClock() {
Thread t = new Thread(this);
t.setPriority(Thread.MIN_PRIORITY);
running = true;
t.start();
}
public void stopClock() {
stopped = true;
}
public void restartClock() {
stopped = false;
}
public void destroyClock() {
stopped = true;
running = false;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub
while (running) {
while (!stopped) {
g.clearRect(0, 0, 320, 240);
g.drawString(time, 100, 30);
System.out.println(time);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
tickSec();
}
while (stopped && running) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}