//cd d:\JavaTry\Book-Tutorial //set CLASSPATH=. //javac Clock.java //appletviewer Clock.java import java.awt.*; import java.util.*; import java.text.DateFormat; import java.applet.Applet; /* */ public class Clock extends Applet implements Runnable { private Thread clockThread = null; public void start() { if (clockThread == null) { // clockThread = new Thread(this); clockThread = new Thread(this, "Clock"); clockThread.start(); } } public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to sleep anymore, // so get back to work } } } public void paint(Graphics g) { // get the time and convert it to a date Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); // format it and display it DateFormat dateFormatter = DateFormat.getTimeInstance(); g.setFont(new Font("Serif", Font.BOLD, 20)); // x=100,y=50の位置から文字列を描画する g.setColor(Color.blue); g.drawString(dateFormatter.format(date), 10, 25); } // overrides Applet's stop method, not Thread's public void stop() { clockThread = null; } }