JAVA Program to Display Digital Clock using Applet

JAVA Program to Display Digital Clock using Applet:


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

public class DigitalClock extends Applet implements Runnable
{

   Thread t = null;
   int hours=0, minutes=0, seconds=0;
   String timeString = "";

   public void init()
   {
      setBackground( Color.yellow);
   }

   public void start()
   {
        t = new Thread( this );
        t.start();
   }


   public void run()
   {
      try
      {
         while(true)
         {

            Calendar cal = Calendar.getInstance();
            hours = cal.get( Calendar.HOUR_OF_DAY );
            if ( hours > 12 )
            hours -= 12;
            minutes = cal.get( Calendar.MINUTE );
            seconds = cal.get( Calendar.SECOND );

            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
            Date date = cal.getTime();
            timeString = formatter.format( date );

            repaint();
            t.sleep( 1000 );
         }
      }
      catch (Exception e) { }
   }


  public void paint( Graphics g )
  {
      g.setColor( Color.blue );
      g.drawString( timeString, 50, 50 );
   }
}

/*
<html>
<body>
<applet code="DigitalClock.class" width="300" height="300">
</applet>
</body>
</html>
*/

SAVE this program as DigitalClock.java and compile this program only. We have integrated the applet code in the comment section of this program. So JAVA will treat it as a comment but applet will recognize the html code and gives the output.

OUTPUT:

JAVA Program to Display Digital Clock using Applet

Leave a Reply

Your email address will not be published. Required fields are marked *

three × 4 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.