Creating Analog Clock in Java

Introduction
 
In this article, we are going to describe how to make an analogue clock using the Graphics class in Java. To make an analogue clock we need to use the Thread and Graphics Classes of Java. The threading concept is used to move the second, minute and hours hands of the clock and the Graphics class is used to create a line and oval and colour of the hands.
 
These are the steps for creating an analogue clock.
 
Step 1: Import the necessary packages.
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.Graphics;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.Locale;  
  7. import javax.swing.JFrame;  
  8. import javax.swing.JPanel; 
Step 2: Define a variable and make the object of the SimpleDate format class. Here the xcenter and ycenter variables are used to define the centre coordinate of the clock and the lastxs and lastys variables used to define the End coordinate of the second's hand and the other variables are lastxm, lastym, lastxh, lastyh are also used for defining the end coordinate of the minutes and hours hands.
  1. Thread thread = null;  
  2. SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());  
  3. Date currentDate;  
  4. int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0,  
  5. lastxh = 0,lastyh = 0
Step 3: We are creating a structured method for the design of the analog clock. In this method we set the digit (3,6,9,12) coordinates and create an oval and set the color of digit and oval.
  1. private void drawStructure(Graphics g)   
  2. {  
  3.  g.setFont(new Font("TimesRoman", Font.BOLD, 20));  
  4.  g.setColor(Color.black);  
  5.  g.fillOval(xcenter - 150, ycenter - 150300300);  
  6.  g.setColor(Color.blue);  
  7.  g.drawString("abhishek dubey"113300);  
  8.  g.setColor(Color.green);  
  9.  g.drawString("9", xcenter - 145, ycenter + 0);  
  10.  g.drawString("3", xcenter + 135, ycenter + 0);  
  11.  g.drawString("12", xcenter - 10, ycenter - 130);  
  12.  g.drawString("6", xcenter - 10, ycenter + 145);  
  13. }   
Step 4: Calculate the seconds, minutes and hours coordinates from the current time by getting the system time. And convert the value into an integer form.
  1. xsecond = (int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * 120 + xcenter);  
  2. ysecond = (int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * 120 + ycenter);  
  3. xminute = (int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 100 + xcenter);  
  4. yminute = (int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 100 + ycenter);  
  5. xhour = (int) (Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + xcenter);  
  6. yhour = (int) (Math.sin((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + ycenter;} 
Step 5: For moving needle logics. Actually in the following line we define some general condition that is found in all types of clocks such as when the seconds hand reaches 12 then the minutes hand moves 1 step and then after moving 5 steps of the minutes hand then 1-stepmoves a hours needle.
  1. g.setColor(Color.magenta);  
  2. if (xsecond != lastxs || ysecond != lastys)   
  3. {  
  4.  g.drawLine(xcenter, ycenter, lastxs, lastys);  
  5. }  
  6. if (xminute != lastxm || yminute != lastym)   
  7. {  
  8.  g.drawLine(xcenter, ycenter - 1, lastxm, lastym);  
  9.  g.drawLine(xcenter - 1, ycenter, lastxm, lastym);  
  10. }  
  11. if (xhour != lastxh || yhour != lastyh)   
  12. {  
  13.  g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);  
  14.  g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);  
  15. }  
  16. g.setColor(Color.magenta);  
  17. g.drawLine(xcenter, ycenter, xsecond, ysecond);   
Step 6: For moving needle minute and hours. this code for set the color of needle and moving forward rest of if conditions.
  1. g.setColor(Color.red);  
  2. g.drawLine(xcenter, ycenter - 1, xminute, yminute);  
  3. g.drawLine(xcenter - 1, ycenter, xminute, yminute);  
  4. g.setColor(Color.green);  
  5. g.drawLine(xcenter, ycenter - 1, xhour, yhour);  
  6. g.drawLine(xcenter - 1, ycenter, xhour, yhour);  
  7. lastxs = xsecond;  
  8. lastys = ysecond;  
  9. lastxm = xminute;  
  10. lastym = yminute;  
  11. lastxh = xhour;  
  12. lastyh = yhour;
Step 7: Define the method of Runable Interface. In the following, all the methods of the Runable interface must be overriden with the definitons of their own functionality within the stop method we pass 100 microseconds and this method throws an interrupt exception so these are put into a try block.
  1. public void start()   
  2. {  
  3.  if (thread == null)   
  4.  {  
  5.   thread = new Thread(this);  
  6.   thread.start();  
  7.  }  
  8. }  
  9. public void stop()   
  10. {  
  11.  thread = null;  
  12. }  
  13. public void run()   
  14. {  
  15.  while (thread != null)   
  16.  {  
  17.   try   
  18.   {  
  19.    Thread.sleep(100);  
  20.   } catch (InterruptedException e) {}  
  21.   repaint();  
  22.  }  
  23.  thread = null;  
  24. }  
  25. public void update(Graphics g)   
  26. {  
  27.  paint(g);  
  28. }   
Step 8: Creating a frame within main methods. In the following code we define three things creating the object of the Color and Frame Clock classes and set the background color and add a content pane on the frame.
  1. public static void main(String args[])   
  2. {  
  3.  JFrame window = new JFrame();  
  4.  Color c = new Color(11873190);  
  5.  window.setBackground(c);  
  6.  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  7.  window.setBounds(00400400);  
  8.  Clock clock = new Clock();  
  9.  window.getContentPane().add(clock);  
  10.  window.setVisible(true);  
  11.  clock.start();  
  12. }  
Complete Code
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.Graphics;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.Locale;  
  7. import javax.swing.JFrame;  
  8. import javax.swing.JPanel;  
  9. public class Clock extends JPanel implements Runnable  
  10. {  
  11.  Thread thread = null;  
  12.  SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());  
  13.  Date currentDate;  
  14.  int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0, lastxh = 0, lastyh = 0;  
  15.  private void drawStructure(Graphics g) {  
  16.   g.setFont(new Font("TimesRoman", Font.BOLD, 20));  
  17.   g.setColor(Color.black);  
  18.   g.fillOval(xcenter - 150, ycenter - 150300300);  
  19.   g.setColor(Color.blue);  
  20.   g.drawString("abhishek dubey"113300);  
  21.   g.setColor(Color.green);  
  22.   g.drawString("9", xcenter - 145, ycenter + 0);  
  23.   g.drawString("3", xcenter + 135, ycenter + 0);  
  24.   g.drawString("12", xcenter - 10, ycenter - 130);  
  25.   g.drawString("6", xcenter - 10, ycenter + 145);  
  26.  }  
  27.  public void paint(Graphics g)   
  28.  {  
  29.   int xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;  
  30.   drawStructure(g);  
  31.   currentDate = new Date();  
  32.   formatter.applyPattern("s");  
  33.   second = Integer.parseInt(formatter.format(currentDate));  
  34.   formatter.applyPattern("m");  
  35.   minute = Integer.parseInt(formatter.format(currentDate));  
  36.   formatter.applyPattern("h");  
  37.   hour = Integer.parseInt(formatter.format(currentDate));  
  38.   xsecond = (int)(Math.cos(second * 3.14 f / 30 - 3.14 f / 2) * 120 + xcenter);  
  39.   ysecond = (int)(Math.sin(second * 3.14 f / 30 - 3.14 f / 2) * 120 + ycenter);  
  40.   xminute = (int)(Math.cos(minute * 3.14 f / 30 - 3.14 f / 2) * 100 + xcenter);  
  41.   yminute = (int)(Math.sin(minute * 3.14 f / 30 - 3.14 f / 2) * 100 + ycenter);  
  42.   xhour = (int)(Math.cos((hour * 30 + minute / 2) * 3.14 f / 180 - 3.14 f / 2) * 80 + xcenter);  
  43.   yhour = (int)(Math.sin((hour * 30 + minute / 2) * 3.14 f / 180 - 3.14 f / 2) * 80 + ycenter);  
  44.   // Erase if necessary, and redraw    
  45.   g.setColor(Color.magenta);  
  46.   if (xsecond != lastxs || ysecond != lastys)   
  47.   {  
  48.    g.drawLine(xcenter, ycenter, lastxs, lastys);  
  49.   }  
  50.   if (xminute != lastxm || yminute != lastym)   
  51.   {  
  52.    g.drawLine(xcenter, ycenter - 1, lastxm, lastym);  
  53.    g.drawLine(xcenter - 1, ycenter, lastxm, lastym);  
  54.   }  
  55.   if (xhour != lastxh || yhour != lastyh)   
  56.   {  
  57.    g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);  
  58.    g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);  
  59.   }  
  60.   g.setColor(Color.magenta);  
  61.   g.drawLine(xcenter, ycenter, xsecond, ysecond);  
  62.   g.setColor(Color.red);  
  63.   g.drawLine(xcenter, ycenter - 1, xminute, yminute);  
  64.   g.drawLine(xcenter - 1, ycenter, xminute, yminute);  
  65.   g.setColor(Color.green);  
  66.   g.drawLine(xcenter, ycenter - 1, xhour, yhour);  
  67.   g.drawLine(xcenter - 1, ycenter, xhour, yhour);  
  68.   lastxs = xsecond;  
  69.   lastys = ysecond;  
  70.   lastxm = xminute;  
  71.   lastym = yminute;  
  72.   lastxh = xhour;  
  73.   lastyh = yhour;  
  74.  }  
  75.  public void start()   
  76.  {  
  77.   if (thread == null)   
  78.   {  
  79.    thread = new Thread(this);  
  80.    thread.start();  
  81.   }  
  82.  }  
  83.  public void stop()   
  84.  {  
  85.   thread = null;  
  86.  }  
  87.  public void run()   
  88.  {  
  89.   while (thread != null)   
  90.   {  
  91.    try   
  92.    {  
  93.     Thread.sleep(100);  
  94.    }   
  95.    catch (InterruptedException e) {}  
  96.    repaint();  
  97.   }  
  98.   thread = null;  
  99.  }  
  100.  public void update(Graphics g)   
  101.  {  
  102.   paint(g);  
  103.  }  
  104.  public static void main(String args[])   
  105.  {  
  106.   JFrame window = new JFrame();  
  107.   Color c = new Color(11873190);  
  108.   window.setBackground(c);  
  109.   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  110.   window.setBounds(00400400);  
  111.   Clock clock = new Clock();  
  112.   window.getContentPane().add(clock);  
  113.   window.setVisible(true);  
  114.   clock.start();  
  115.  }  
  116. }   
OUTPUT