How To Create Analog Clock Using Java Applet

Introduction

 
In this article, we discuss how to create an analog clock using a Java applet. We must use Java applet components like the graphics class and we need to implement a runnable interface to start, run, suspend, stop etcetera the thread. Threading is used to move the hands of our clock and the Graphics class is used to design the line, strings, etcetera.
 

Analog Clock using a Java applet

 
There are certain requirements for the creation of the analog clock using a Java applet.
 
Step 1: Import the following packages:
  1. import java.util.*;  
  2. import java.text.*;  
  3. import java.applet.*;  
  4. import java.awt.*;  
Step 2: We need to define a variable and make the object of the SimpleDateFormat class. In other words:
  1. Thread thr=null;  
  2. SimpleDateFormat frmatter=new SimpleDateFormat("hh:mm:ss", Locale.getDefault());  
  3. Date d=clndr.getTime();  
For drawing and running the second hand, we use the drawHand method, as in the following:
  1. void drawHand(double angle, int radius, Graphics grp)  
  2. {  
  3.     angle-=0.5*Math.PI;  
  4.     int a=(int)(radius*Math.cos(angle));  
  5.     int b=(int)(radius*Math.sin(angle));  
  6.     grp.drawLine(clkwidth/2,clkheight/2,clkwidth/2+a,clkheight/2+b);  
  7. }  
The following figure introduces the wedge structure:
 
wedge.jpg
  1. void drawWedge(double angle, int radius, Graphics grp)  
  2. {  
  3.     angle -= 0.5 * Math.PI;  
  4.     int a = (int)(radius * Math.cos(angle));  
  5.     int b = (int)(radius * Math.sin(angle));  
  6.     angle += 2 * Math.PI / 3;  
  7.     int a2 = (int)(5 * Math.cos(angle));  
  8.     int b2 = (int)(5 * Math.sin(angle));  
  9.     angle += 2 * Math.PI / 3;  
  10.     int a3 = (int)(5 * Math.cos(angle));  
  11.     int b3 = (int)(5 * Math.sin(angle));  
  12.     grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a, clkheight / 2 + b);  
  13.     grp.drawLine(clkwidth / 2 + a3, clkheight / 2 + b3, clkwidth / 2 + a, clkheight / 2 + b);  
  14.     grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a3, clkheight / 2 + b3);  
  15. }  
Step 3: We are creating a structured method using the graphics class for the design of the analog clock, as in the following:
  1. public void paint(Graphics grp)  
  2. {  
  3.     grp.setColor(Color.gray);  
  4.     drawWedge(2 * Math.PI * clkhours / 12, clkwidth / 5, grp);  
  5.     drawWedge(2 * Math.PI * clkminutes / 60, clkwidth / 3, grp);  
  6.     drawHand(2 * Math.PI * clkseconds / 60, clkwidth / 2, grp);  
  7.     grp.setColor(Color.white);  
  8.     grp.drawString(clkString, 10, clkheight - 10);  
  9.     grp.drawString("C-Sharpcorner.com"113300);  
  10. }  
Step 4: Define the method of the runnable interface. In that the all method of runnable interface is overridden. See the following:
  1. public void start()  
  2. {  
  3.     if (thr == null)  
  4.     {  
  5.         thr = new Thread(this);  
  6.         thr.setPriority(Thread.MIN_PRIORITY);  
  7.         threadSuspended = false;  
  8.         thr.start();  
  9.     } else  
  10.     {  
  11.         if (threadSuspended)  
  12.         {  
  13.             threadSuspended = false;  
  14.             synchronized(this)  
  15.             {  
  16.                 notify();  
  17.             }  
  18.         }  
  19.     }  
  20. }  
  21. public void stop()  
  22. {  
  23.     threadSuspended = true;  
  24. }  
  25. public void run()  
  26. {  
  27.     try  
  28.     {  
  29.         while (true)  
  30.         {  
  31.             Calendar clndr = Calendar.getInstance();  
  32.             clkhours = clndr.get(Calendar.HOUR_OF_DAY);  
  33.             if (clkhours > 12) clkhours -= 12;  
  34.             clkminutes = clndr.get(Calendar.MINUTE);  
  35.             clkseconds = clndr.get(Calendar.SECOND);  
  36.             SimpleDateFormat frmatter = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());  
  37.             Date d = clndr.getTime();  
  38.             clkString = frmatter.format(d);  
  39.             if (threadSuspended)  
  40.             {  
  41.                 synchronized(this)  
  42.                 {  
  43.                     while (threadSuspended)  
  44.                     {  
  45.                         wait();  
  46.                     }  
  47.                 }  
  48.             }  
  49.             repaint();  
  50.             thr.sleep(1000);  
  51.         }  
  52.     }  
  53. }  
Complete code
  1. import java.util.*;  
  2. import java.text.*;  
  3. import java.applet.*;  
  4. import java.awt.*;  
  5. public class SimpleAnalogClock extends Applet implements Runnable  
  6. {  
  7.     int clkhours = 0, clkminutes = 0, clkseconds = 0;  
  8.     String clkString = "";  
  9.     int clkwidth, clkheight;  
  10.     Thread thr = null;  
  11.     boolean threadSuspended;  
  12.     public void init()  
  13.     {  
  14.         clkwidth = getSize().width;  
  15.         clkheight = getSize().height;  
  16.         setBackground(Color.black);  
  17.     }  
  18.     public void start()  
  19.     {  
  20.         if (thr == null)  
  21.         {  
  22.             thr = new Thread(this);  
  23.             thr.setPriority(Thread.MIN_PRIORITY);  
  24.             threadSuspended = false;  
  25.             thr.start();  
  26.         } else  
  27.         {  
  28.             if (threadSuspended)  
  29.             {  
  30.                 threadSuspended = false;  
  31.                 synchronized(this)  
  32.                 {  
  33.                     notify();  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38.     public void stop()  
  39.     {  
  40.         threadSuspended = true;  
  41.     }  
  42.     public void run()  
  43.     {  
  44.         try  
  45.         {  
  46.             while (true)  
  47.             {  
  48.                 Calendar clndr = Calendar.getInstance();  
  49.                 clkhours = clndr.get(Calendar.HOUR_OF_DAY);  
  50.                 if (clkhours > 12) clkhours -= 12;  
  51.                 clkminutes = clndr.get(Calendar.MINUTE);  
  52.                 clkseconds = clndr.get(Calendar.SECOND);  
  53.                 SimpleDateFormat frmatter = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());  
  54.                 Date d = clndr.getTime();  
  55.                 clkString = frmatter.format(d);  
  56.                 if (threadSuspended)  
  57.                 {  
  58.                     synchronized(this)  
  59.                     {  
  60.                         while (threadSuspended)  
  61.                         {  
  62.                             wait();  
  63.                         }  
  64.                     }  
  65.                 }  
  66.                 repaint();  
  67.                 thr.sleep(1000);  
  68.             }  
  69.         } catch (Exception e)  
  70.         {}  
  71.     }  
  72.     void drawHand(double angle, int radius, Graphics grp)  
  73.     {  
  74.         angle -= 0.5 * Math.PI;  
  75.         int a = (int)(radius * Math.cos(angle));  
  76.         int b = (int)(radius * Math.sin(angle));  
  77.         grp.drawLine(clkwidth / 2, clkheight / 2, clkwidth / 2 + a, clkheight / 2 + b);  
  78.     }  
  79.     void drawWedge(double angle, int radius, Graphics grp)  
  80.     {  
  81.         angle -= 0.5 * Math.PI;  
  82.         int a = (int)(radius * Math.cos(angle));  
  83.         int b = (int)(radius * Math.sin(angle));  
  84.         angle += 2 * Math.PI / 3;  
  85.         int a2 = (int)(5 * Math.cos(angle));  
  86.         int b2 = (int)(5 * Math.sin(angle));  
  87.         angle += 2 * Math.PI / 3;  
  88.         int a3 = (int)(5 * Math.cos(angle));  
  89.         int b3 = (int)(5 * Math.sin(angle));  
  90.         grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a, clkheight / 2 + b);  
  91.         grp.drawLine(clkwidth / 2 + a3, clkheight / 2 + b3, clkwidth / 2 + a, clkheight / 2 + b);  
  92.         grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a3, clkheight / 2 + b3);  
  93.     }  
  94.     public void paint(Graphics grp)  
  95.     {  
  96.         grp.setColor(Color.gray);  
  97.         drawWedge(2 * Math.PI * clkhours / 12, clkwidth / 5, grp);  
  98.         drawWedge(2 * Math.PI * clkminutes / 60, clkwidth / 3, grp);  
  99.         drawHand(2 * Math.PI * clkseconds / 60, clkwidth / 2, grp);  
  100.         grp.setColor(Color.white);  
  101.         grp.drawString(clkString, 10, clkheight - 10);  
  102.         grp.drawString("C-Sharpcorner.com"113300);  
  103.     }  
  104. }  
  105. /*  
  106. <applet code="SimpleAnalogClock.class" width="350" height="350">  
  107. </applet>  
  108. /  
Output
 
Analog Clock in Java
 
After pressing Enter we see the following output, as in the following:
 
Analog Clock in Java
 
After one minute the clock will show:
 
Analog Clock in Java