import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Clock
{
JFrame frame = new JFrame("Clock");
Drawing draw = new Drawing();
TimeKeeper time = new TimeKeeper();
int hours=0, minutes=0, seconds=0;
public Clock()
{
frame.add(draw,"Center");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
time.start();
}
public static void main (String[] args)
{
new Clock();
}
class Drawing extends JComponent
{
public void paint(Graphics g)
{
g.drawString(hours + ":" + minutes + ":" + seconds, 100,100);
}
}
class TimeKeeper extends Thread
{
public void run()
{
try
{
while (true)
{
sleep(1000); // update the time once a second
seconds++;
if (seconds > 59)
{
seconds = 0;
minutes++;
if (minutes > 59)
{
minutes = 0;
hours++;
}
}
draw.repaint();
}
}
catch (InterruptedException e)
{
}
}
}
}
|