FileDocCategorySizeDatePackage
Clock.javaAPI DocExample1327Mon Mar 29 20:03:14 BST 1999None

Clock

public class Clock extends JComponent
A simple Clock

Fields Summary
protected DecimalFormat
tflz
protected DecimalFormat
tf
protected boolean
done
Constructors Summary
public Clock()


	  
		new Thread(new Runnable() {
			public void run() {
				while (!done) {
					Clock.this.repaint();	// request a redraw
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e){ /* do nothing*/ }
				}
			}
		}).start();
		tf = new DecimalFormat("#0");
		tflz = new DecimalFormat("00");
	
Methods Summary
public java.awt.DimensiongetMinimumSize()

		return new Dimension(50, 10);
	
public java.awt.DimensiongetPreferredSize()

		return new Dimension(100, 30);
	
public voidpaint(java.awt.Graphics g)

		Calendar myCal = Calendar.getInstance();
		StringBuffer sb = new StringBuffer();
		sb.append(tf.format(myCal.get(Calendar.HOUR)));
		sb.append(':");
		sb.append(tflz.format(myCal.get(Calendar.MINUTE)));
		sb.append(':");
		sb.append(tflz.format(myCal.get(Calendar.SECOND)));
		String s = sb.toString();
		FontMetrics fm = getFontMetrics(getFont());
		int x = (getSize().width - fm.stringWidth(s))/2;
		// System.out.println("Size is " + getSize());
		g.drawString(s, x, 10);
	
public voidstop()

		done = true;