0% found this document useful (0 votes)
97 views2 pages

Digital Clock

This Java program creates a digital clock applet that uses a thread to continuously update the current hour, minute and second by getting the time from the Calendar class. It draws the digital time as a string in the paint method with the font and location specified. The thread sleeps for 1 second between each repaint to continuously update the displayed time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views2 pages

Digital Clock

This Java program creates a digital clock applet that uses a thread to continuously update the current hour, minute and second by getting the time from the Calendar class. It draws the digital time as a string in the paint method with the font and location specified. The thread sleeps for 1 second between each repaint to continuously update the displayed time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Digital Clock

Program:
import java.util.Calendar;
import java.awt.*;
import java.applet.*;
/*<applet code="MyClock" width=100 height=100></applet>*/
public class MyClock extends Applet implements Runnable
{
Thread t;
int hh,mm,ss;
Font myf=new Font("Timse New Roman",Font.BOLD,20);
public void init()
{
if(t==null)
{
t=new Thread(this);
t.start();
}
}
public void run()
{
for(;;)
{
Calendar tm=Calendar.getInstance();
hh=tm.get(Calendar.HOUR);
mm=tm.get(Calendar.MINUTE);
ss=tm.get(Calendar.SECOND);
repaint();
try
{
t.sleep(1000);
}
catch(Exception e)
{
}
}
}
public void paint(Graphics s)
{
String now="";
s.setFont(myf);
now=String.valueOf(hh)+":"+String.valueOf(mm)+":"+String.valueOf(ss);
s.drawString(now,50,50);
}
}

Output:
D:\java\Applet>javac MyClock.java
D:\java\Applet>AppletViewer MyClock.java

You might also like