Digital watch in java.

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 21, 2024
hello friend's
Just completed a program for a digital watch in java...Check out this code.
import java.awt.*;
import java.applet.*;
/*<APPLET CODE="kk.class" HEIGHT=200 WIDTH=300></APPLET>*/
public class kk extends Applet implements Runnable
{
int x=00;
int y=00;
int z=00;
Thread t;
public void time()
{
if(x!=59)
{
x=x+1;
}
else
{
if(y!=59)
{
y=y+1;
x=00;
}
else
{
if(z!=59)
{
z=z+1;
y=00;x=00;
}
else
{
z=00;y=00;x=00;
}
}
}
}
public void init()
{
setBackground(Color.cyan);
}

public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(t!=null)
{
try
{
repaint();
t.sleep(1000);
time();
}
catch(InterruptedException e)
{
System.out.println("Thread interrupted ");
}
}
}
public void paint(Graphics g)
{
Font f=new Font("ARIEL",Font.BOLD+Font.ITALIC,60);
String p=Integer.toString(x);
String q=Integer.toString(y);
String r=Integer.toString(z);
setFont(f);
g.drawString(r+":"+q+":"+p,20,70);
showStatus(r+":"+q+":"+p);
}
}
save it as kk.java.
Command to compile this code: javac kk.java
Command to execute the class file created by compiler: appletviewer kk.java

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Aug 31, 2010

    nice code mohit
    :clap::clap:
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 2, 2010

    Thanks goyal...!!
  • sushant005

    sushant005

    @sushant005-tyt4WK Sep 4, 2010

    hello CEans here is my code for digital watch,
    Execute and see is it working fine or need some correction or any implementation.
    I have not use any predefined method.

    import java.awt.*;
    import java.util.*;
    import java.applet.*;

    public class watch extends Applet implements Runnable
    {
    Thread t;
    int i,j,k;
    public void init()
    {
    setBackground(Color.red);
    }
    public void start()
    {
    t=new Thread(this);
    t.start();
    }
    public void run()
    {

    for( i=0;i<24;i++)
    for(j=0;j<60;j++)
    for(k=0;k<60;k++){
    try{
    repaint();
    Thread.sleep(1000);
    }
    catch(Exception e){}
    }
    }

    public void paint(Graphics g)
    {
    String s=String.valueOf(j);
    String t=String.valueOf(i);
    String h=String.valueOf(k);
    g.drawString(h,80,30);
    g.drawString(" : ",70,30);
    g.drawString(s,60,30);
    g.drawString(" : ",50,30);
    g.drawString(t,35,30);
    }
    }
    /*<applet code="watch.java" width="400" height="500"></applet>*/