Java Swing Analog Clock Example

  • Updated date Sep 17, 2019
  • 146.6k
  • 4

In this article we are going to describe how to make an analog clock using the Graphics class 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 - 150 , 300 , 300 );
  6.  g.setColor(Color.blue);
  7.  g.drawString("abhishek dubey" , 113 , 300 );
  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( 118 , 73 , 190 );
  5.  window.setBackground(c);
  6.  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.  window.setBounds(0 , 0 , 400 , 400 );
  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 - 150 , 300 , 300 );
  19.   g.setColor(Color.blue);
  20.   g.drawString("abhishek dubey" , 113 , 300 );
  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.   g.setColor(Color.magenta);
  45. if  (xsecond != lastxs || ysecond != lastys)
  46.   {
  47.    g.drawLine(xcenter, ycenter, lastxs, lastys);
  48.   }
  49. if  (xminute != lastxm || yminute != lastym)
  50.   {
  51.    g.drawLine(xcenter, ycenter -1 , lastxm, lastym);
  52.    g.drawLine(xcenter -1 , ycenter, lastxm, lastym);
  53.   }
  54. if  (xhour != lastxh || yhour != lastyh)
  55.   {
  56.    g.drawLine(xcenter, ycenter -1 , lastxh, lastyh);
  57.    g.drawLine(xcenter -1 , ycenter, lastxh, lastyh);
  58.   }
  59.   g.setColor(Color.magenta);
  60.   g.drawLine(xcenter, ycenter, xsecond, ysecond);
  61.   g.setColor(Color.red);
  62.   g.drawLine(xcenter, ycenter -1 , xminute, yminute);
  63.   g.drawLine(xcenter -1 , ycenter, xminute, yminute);
  64.   g.setColor(Color.green);
  65.   g.drawLine(xcenter, ycenter -1 , xhour, yhour);
  66.   g.drawLine(xcenter -1 , ycenter, xhour, yhour);
  67.   lastxs = xsecond;
  68.   lastys = ysecond;
  69.   lastxm = xminute;
  70.   lastym = yminute;
  71.   lastxh = xhour;
  72.   lastyh = yhour;
  73.  }
  74. public void  start()
  75.  {
  76. if  (thread == null )
  77.   {
  78.    thread =new  Thread( this );
  79.    thread.start();
  80.   }
  81.  }
  82. public void  stop()
  83.  {
  84.   thread =null ;
  85.  }
  86. public void  run()
  87.  {
  88. while  (thread != null )
  89.   {
  90. try
  91.    {
  92.     Thread.sleep(100 );
  93.    }
  94. catch  (InterruptedException e) {}
  95.    repaint();
  96.   }
  97.   thread =null ;
  98.  }
  99. public void  update(Graphics g)
  100.  {
  101.   paint(g);
  102.  }
  103. public static void  main(String args[])
  104.  {
  105.   JFrame window =new  JFrame();
  106.   Color c =new  Color( 118 , 73 , 190 );
  107.   window.setBackground(c);
  108.   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109.   window.setBounds(0 , 0 , 400 , 400 );
  110.   Clock clock =new  Clock();
  111.   window.getContentPane().add(clock);
  112.   window.setVisible(true );
  113.   clock.start();
  114.  }
  115. }

OUTPUT

lancasterduchich.blogspot.com

Source: https://www.c-sharpcorner.com/UploadFile/433c33/creating-analog-clock-in-java/

0 Response to "Java Swing Analog Clock Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel