programming-examples/java/Applets_AWT/Clock Applet.java
2019-11-15 12:59:38 +01:00

43 lines
950 B
Java

Clock Applet
import java.applet.*;
import java.awt.*;
import java.util.Date;
public class ClockApplet extends Applet implements Runnable {
Thread timerThread;
/* Applet Lifestyle Methods */
public void start() {
timerThread = new Thread(this, "Clock");
timerThread.start();
}
public void stop() {
if (timerThread == null)
return;
timerThread.stop();
timerThread = null;
}
/* Runnable interface method */
public void run() {
while (timerThread != null) {
repaint(); // request a redraw
try {
timerThread.sleep(1000);
} catch (InterruptedException e){ /* do nothing*/ }
}
}
/* AWT method */
public void paint(Graphics g) {
Date d = new Date();
g.drawString(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(), 1, 10);
}
}
/*
<APPLET CODE="ClockApplet.class" WIDTH=60 HEIGHT=12>
(which you cant see until you get a Java-powered browser!)
</APPLET>
*/