Java Program for stop clock showing seconds, minutes & hours, Java Program clock
import java.applet.*;
import java.awt.*;
/*
<applet code="Clock" width=250 height=100>
</applet>
*/
public class Clock extends Applet implements Runnable
{
int counter;
int min;
int hr;
int sec;
Thread t;
public void init()
{
counter=0;
int sec=0;
int min=0;
int hr=0;
t=new Thread(this);
t.start();
}
public void run()
{
try
{
while(true)
{
setForeground(Color.green);
repaint();
Thread.sleep(25);
counter++;
if(counter == 60)
{
counter=0;
sec++;
setForeground(Color.red);
setBackground(Color.blue);
if(sec==60)
{
setForeground(Color.cyan);
setBackground(Color.red);
sec=0;
min++;
if(min==60)
{
min=0;
hr++;
if(hr==12)
{ hr=0;
}
}
}
}
}
}
catch(Exception e)
{
}
}
public void paint(Graphics g)
{
g.setFont(new Font("Arial",Font.BOLD,36));
FontMetrics fm=g.getFontMetrics();
String str="=>> "+hr+":"+min+":"+sec+":"+counter;
Dimension d=getSize();
int x=(d.width-fm.stringWidth(str))/2;
g.drawString(str,x,d.height/2);
}
}
No comments:
Post a Comment