Create Digital Clock in Java Application Program

To run a digital clock by using Java applet
Source code programing Algorithm
Step 1: Import necessary packages.
Step 2: Write applet code which should specify width and height of applet window.
Step 3: Create a class MyClock implement the interface runnable.
Step 4: Create an object for thread and start it.
Step 5: Create an object for class Calendar.
Step 6: Get hour, minute and seconds using that object and assign to variables.
Step 7: Print that variables for every second using sleep method.
Step 8: Compile and execute the program.
How to create Digital Clock Java

Source code Programming Algorithm in Java IT1403
import java.util.Calendar;
import java.awt.*;
import java.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);
}
}

Related post