1000ミリ秒ごとにThreadで再表示させて、まるで、数字が動いているかのようにみせます。run()、start()、stop()の三つのメソッドを記述しています。sleep()メソッドで、clockThreadスレッドを1000ミリ秒間、休止させています。
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
class AnimationClockTestFrame extends Frame
implements Runnable{
private Thread clockThread=null;
public AnimationClockTestFrame(){
setSize(480, 80);
setForeground(Color.BLUE);
setFont(new Font("Monospaced", Font.BOLD, 24));
}
public void paint(Graphics g){
Date date=new Date();
g.drawString(date.toString(), 50, 60);
}
public void start(){
if(clockThread==null){
clockThread=new Thread(this, "Clock");
clockThread.start();
}
}
public void stop(){
clockThread=null;
}
public void run(){
while(Thread.currentThread()==clockThread){
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
}
}
public class watch{
public static void main(String[] args){
AnimationClockTestFrame frame=new AnimationClockTestFrame();
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setVisible(true);
frame.start();
}
}