In this article, we will be using two main concepts of Java. They are thread class and graphics class of Java. The functions of using these classes of Java are as follows:
- The thread class of Java is used to make the second hand, minute hand and hour hand of the clock move.
- The graphics class of Java is used to create a line and oval and color of the hands.
In the process of creating the analog clock, we need to follow a step by step procedure. The process is divided into eight steps. In the first step, the process of importing the necessary packages is done. This initiation step is very important.
Listing1: Shows the code for importing the necessary packages
In the second step, we will be defining a variable and make the object of SimpleDate format class. After this, the center variables i.e., xcenter and ycenter variables are used to define the center variables. In this step, the variables that are used for defining the end coordinate of the second hand are also defined and they are known as lastxs and lastys. Apart from this, other variables such as lastxm, lastym, lastxh, lastyh are also used for defining the end coordinate of the minutes and hours hands.
Listing 2: Shows the code for defining the variables
Thread thread = null;
SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());
int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0,
lastxh = 0,lastyh = 0;
In the third step, a structured method for designing of the analog clock is constructed. In this structured method, the digit coordinate such as three, six, nine and twelve are set and finally an oval is created and the colour of digits and oval is also set.
Listing 3: Shows the coding for the structured method
private void drawStructure(Graphics g)
g.setFont(new Font("TimesRoman", Font.BOLD, 20));
g.fillOval(xcenter - 150, ycenter - 150, 300, 300);
g.drawString("abhishek dubey" ,113,300);
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);
In the fourth step, the seconds, minutes and hours coordinates are calculated from the current time and this calculation is done by system time and finally, the values are converted into an integer form.
Listing 4: Shows the coding for converting the values 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;}
With this step, our half job is done. Now in the fifth step, we will be setting the moving needle logics. These conditions are found out in all clocks. These include 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 an hours needle.
Listing 5: Shows the code for defining the moving needle logics
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.drawLine(xcenter, ycenter, xsecond, ysecond);
In the sixth step, we will be setting the colors of the needle in order to differentiate them easily.
Listing 6: Shows the code for setting the colors of the needle
g.drawLine(xcenter, ycenter - 1, xminute, yminute);
g.drawLine(xcenter - 1, ycenter, xminute, yminute);
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;
In the seventh step, the method of Runable interface is defined. In this, 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.
Listing 7: Shows the code for defining the method of runnable interface
public void start()
if (thread == null)
thread = new Thread(this);
public void stop()
thread = null;
public void run()
while (thread != null)
catch (InterruptedException e)
thread = null;
public void update(Graphics g)
Finally, in the last step, a frame is created within the main methods. In this, we will be defining three things. They are as follows:
- Creating the object of the color and frame clock classes.
- Setting the background color.
- Finally, adding a content pane on the frame.
Listing 8: Shows the coding for doing the three things mentioned in the last step
public static void main(String args)
JFrame window = new JFrame();
Color c=new Color(118,73,190);
window.setBounds(0, 0, 400, 400);
Clock clock = new Clock();
In this article, we have learnt to develop a very simple application of constructing an analog clock in Java. For this we will be using the thread and graphic classes of Java.