How to display Clock using Applet

An applet is a small program typically embedded within the web page which is used to create a dynamic and interactive application.Each applet that is created must be subclass of the Applet class, contained within the java.applet package. This class contains methods which govern the life and behavior of the Applet.

 

import java.applet.*;

import java.awt.*;

import java.util.*;

public class ClockkApplet extends Applet implements Runnable

{

   Image ima;

   Thread t,t1;

   public void start()

{

     t = new Thread(this);

     t.start();

   }

   public void run()

{

     t1 = Thread.currentThread();

     while(t1 == t)

{

         repaint();

         try

{

           t1.sleep(1000);  

         }

         catch(InterruptedException e)

{

}

     }

   }

public void init()

{

ima=getImage(getCodeBase(),"wel.jpg");

}

   public void paint(Graphics grap)

{

grap.drawImage(ima,10,30,this);

        Calendar calend = new GregorianCalendar();

     String hourr = String.valueOf(calend.get(Calendar.HOUR));

     String minutee = String.valueOf(calend.get(Calendar.MINUTE));

     String secoond = String.valueOf(calend.get(Calendar.SECOND));

     Font fon=new Font("Arial",Font.BOLD/Font.ITALIC,20);

     grap.setFont(fon);

    

   Color c=new Color(106,34,99);

     grap.setColor(c);

    

              

       grap.drawString(hourr +" : "+ minutee +" : "+ secoond, 150, 110);

       grap.drawString(" Value",56,220);

       grap.drawString("Ur",120,205);

       grap.drawString("Time",150,220);

      

   }

}

/*<applet code="ClockkApplet.class" height=300 width=300></applet>*/

 display clock using Applet

Explanation..

In above example ,ClockkApplet is a class which implements the runnable interface. The runnable interface consists of a single method run() which is required for implementing thread. The thread will be activated by calling the start().GregorianCalendar is used to show time.