Write a applet program that simulates a digital clock.

//Program for implementing Digital Clock
import java.applet.*;
import java.awt.*;
import java.util.*;
/*<applet code="Digital" width=350 height=50></applet>*/
public class Digital extends Applet implements Runnable {           
    String msg = "";           
    public void init()            {                       
        setBackground(Color.pink);                       
        setFont(new Font("Courier New", Font.BOLD, 20));                                  
    }           
    public void start()            {                       
        Thread t = new Thread(this);                       
        t.start();           
    }           
    public void run()            {                       
        for (;;)                        {                                   
            try {                                               
                Calendar c = Calendar.getInstance();                                                                         
                Thread.sleep(1000);                                               
                msg = String.valueOf(c.get(Calendar.HOUR)) + " : " + String.valueOf(c.get(Calendar.MINUTE)) + " : " + String.valueOf(c.get(Calendar.SECOND));                                               
                repaint();                                      
            }                                     
            catch (InterruptedException e) { }                       
        }           
    }           
    public void paint(Graphics g)            {                       
        g.drawString("Time -", 30, 30);                       
        g.drawString(msg, 140, 30);           
    }


}
 
 
Output:
 
 
 

 

                           

 

Leave a Reply

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

Enable Notifications OK No thanks