 |
|
|
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 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize(300, 100);
show();
showTime();
}
private void showTime()
{
GregorianCalendar cal;
int hour, min, sec;
//never ending loop
while( true )
{
//get time info
cal = new GregorianCalendar();
hour = cal.get( Calendar.HOUR_OF_DAY );
min = cal.get( Calendar.MINUTE );
sec = cal.get( Calendar.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);
}
catch( Exception e ) {}
}
}
public void paint( Graphics g )
{
//clear and display the time
if( time != null )
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight() );
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString( time, 30, 80 );
}
}
public static void main(String[] args)
{
new DigitalWatch();
}
}
|
|
|
 |