Start Search Contents Index Links About
DigitalWatch.java
/*
 * Author: Havard Rast Blok
 * E-mail:  
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import java.util.*;
import javax.swing.*;

/**
 * A digital watch
 */
public class DigitalWatch extends JFrame
{
  private String time;
  private Font font;
  
  public DigitalWatch()
  {
    super("My watch");
    
    font = new Font("SansSerif", Font.PLAIN, 40 );
    
    setDefaultCloseOperationJFrame.EXIT_ON_CLOSE );
    setSize(300100);
    show();
    
    showTime();
  }
  
  private void showTime()
  {
    GregorianCalendar cal;
    int hour, min, sec;
    
    //never ending loop
    whiletrue )
    {
      //get time info
      cal = new GregorianCalendar();
      hour = cal.getCalendar.HOUR_OF_DAY );
      min = cal.getCalendar.MINUTE );
      sec = cal.getCalendar.SECOND );
      
      //this will not print leading zeros
      time = ""+hour+" : "+min+" : "+sec;
    
      //paint frame and let repaint thread run
      repaint();
      Thread.yield();
      
      //wait for small amount of time
      try
      {
        Thread.sleep(100);
      }
      catchException e ) {}
    }
  }
  
  public void paintGraphics g )
  {
    //clear and display the time
    iftime != null )
    {
      g.setColor(Color.WHITE);
      g.fillRect(00, getWidth(), getHeight() );
      g.setColor(Color.BLACK);
      g.setFont(font);
      g.drawStringtime, 3080 );
    }
  }
  
  public static void main(String[] args)
  {
     new DigitalWatch();
  }
  
}


site: Håvard Rast Blok
mail:
updated: 27 July 2007