Creating Analog Clock in Java

Introduction

In this article we are going to describe how to make an analog clock using the Graphics class in Java. To make an analog 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 Graphic class is used to create a line and oval and color of the hands.

These are the steps for creating an analog clock.

Step 1: Import the necessary packages.

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import javax.swing.JFrame;

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 center coordinate of the clock and the lastxs and lastys vriables used to define the End coordinate of the seconds hand and the other variables are lastxm, lastym, lastxh, lastyh are also used for defining the end coordinate of the minutes and hours hands.

Thread thread = null;

SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());

Date currentDate;

int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0,
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.

private void drawStructure(Graphics g)

{
g.setFont(
new Font("TimesRoman", Font.BOLD, 20));

g.setColor(Color.black);

g.fillOval(xcenter - 150, ycenter - 150, 300, 300);

g.setColor(Color.blue);

g.drawString("abhishek dubey" ,113,300);

g.setColor(Color.green);

g.drawString("9", xcenter - 145, ycenter +0);

g.drawString("3", xcenter + 135, ycenter + 0);

g.drawString("12", xcenter - 10, ycenter - 130);

g.drawString("6", xcenter - 10, ycenter + 145);

}

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.

xsecond = (int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * 120 + xcenter);

ysecond = (int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * 120 + ycenter);

xminute = (int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 100 + xcenter);

yminute = (int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 100 + ycenter);

xhour = (int) (Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + xcenter);

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.

g.setColor(Color.magenta);

if (xsecond != lastxs || ysecond != lastys)

{

g.drawLine(xcenter, ycenter, lastxs, lastys);

}

if (xminute != lastxm || yminute != lastym)

{

g.drawLine(xcenter, ycenter - 1, lastxm, lastym);

g.drawLine(xcenter - 1, ycenter, lastxm, lastym);

}

if (xhour != lastxh || yhour != lastyh)

{

g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);

g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);

}
g.setColor(Color.magenta);
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.

g.setColor(Color.red);

g.drawLine(xcenter, ycenter - 1, xminute, yminute);

g.drawLine(xcenter - 1, ycenter, xminute, yminute);

g.setColor(Color.green);

g.drawLine(xcenter, ycenter - 1, xhour, yhour);

g.drawLine(xcenter - 1, ycenter, xhour, yhour);

lastxs = xsecond;

lastys = ysecond;

lastxm = xminute;

lastym = yminute;

lastxh = xhour;

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.

public void start()
{

if (thread == null)
{

thread = new Thread(this);

thread.start();

}

}
public void stop()

{
thread =
null;

}
public void run()

{

while (thread != null)
{

try
{
Thread.sleep(100);

}
catch
(InterruptedException e)
{}

repaint();

}

thread = null;

}
public void update(Graphics g)
{

paint(g);

}
 

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.

public static void main(String args[])
{

JFrame window = new JFrame();
Color c=new Color(118,73,190);

window.setBackground(c);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setBounds(0, 0, 400, 400);

Clock clock = new Clock();

window.getContentPane().add(clock);

window.setVisible(true);

clock.start();

}

Complete Code

import java.awt.Color;

import java.awt.Font;
import
java.awt.Graphics;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
java.util.Locale;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
public
class Clock extends JPanel implements Runnable
{

Thread thread =
null;
SimpleDateFormat formatter =
new SimpleDateFormat("s", Locale.getDefault());
Date currentDate;

int
xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0, lastxh = 0,lastyh = 0;
private
void drawStructure(Graphics g){
g.setFont(
new Font("TimesRoman", Font.BOLD, 20));
g.setColor(Color.black);

g.fillOval(xcenter - 150, ycenter - 150, 300, 300);

g.setColor(Color.blue);

g.drawString(
"abhishek dubey" ,113,300);
g.setColor(Color.green);

g.drawString(
"9", xcenter - 145, ycenter +0);
g.drawString(
"3", xcenter + 135, ycenter + 0);
g.drawString(
"12", xcenter - 10, ycenter - 130);
g.drawString(
"6", xcenter - 10, ycenter + 145);
}

public
void paint(Graphics g) {
int
xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;drawStructure(g);
currentDate =
new Date();
formatter.applyPattern(
"s");
second = Integer.parseInt(formatter.format(currentDate));

formatter.applyPattern(
"m");
minute = Integer.parseInt(formatter.format(currentDate));

formatter.applyPattern(
"h");
hour = Integer.parseInt(formatter.format(currentDate));

xsecond = (
int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * 120 + xcenter);
ysecond = (
int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * 120 + ycenter);
xminute = (
int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 100 + xcenter);
yminute = (
int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 100 + ycenter);
xhour = (
int) (Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + xcenter);
yhour = (
int) (Math.sin((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + ycenter);
// Erase if necessary, and redraw

g.setColor(Color.magenta);

if
(xsecond != lastxs || ysecond != lastys)
{

g.drawLine(xcenter, ycenter, lastxs, lastys);

}

if
(xminute != lastxm || yminute != lastym)
{

g.drawLine(xcenter, ycenter - 1, lastxm, lastym);

g.drawLine(xcenter - 1, ycenter, lastxm, lastym);

}

if
(xhour != lastxh || yhour != lastyh)
{

g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);

g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);

}

g.setColor(Color.magenta);

g.drawLine(xcenter, ycenter, xsecond, ysecond);

g.setColor(Color.red);

g.drawLine(xcenter, ycenter - 1, xminute, yminute);

g.drawLine(xcenter - 1, ycenter, xminute, yminute);

g.setColor(Color.green);

g.drawLine(xcenter, ycenter - 1, xhour, yhour);

g.drawLine(xcenter - 1, ycenter, xhour, yhour);

lastxs = xsecond;

lastys = ysecond;

lastxm = xminute;

lastym = yminute;

lastxh = xhour;

lastyh = yhour;

}

  public void start() {
   
if (thread == null) {

      thread = new Thread(this);
      thread.start();

    }

  }

  public void stop()
{

thread = null;
}

  public void run() {
     while (thread != null) {
     
try {
        Thread.sleep(100);
      }
catch (InterruptedException e) {
      }
      repaint();
    }
    thread =
null;
  }

  public void update(Graphics g) {
    paint(g);
  }

  public static void main(String args[]) {
     JFrame window =
new JFrame();
      Color c=
new Color(118,73,190);
      window.setBackground(c);

      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      window.setBounds(0, 0, 400, 400);

      Clock clock =
new Clock();
      window.getContentPane().add(clock);

      window.setVisible(
true);
      clock.start();

   }

}

 

OUTPUT



cmdclock.gif


analogclock.gif

Up Next
    Ebook Download
    View all
    Learn
    View all