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:
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
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;
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());
Date currentDate;
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.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);
}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
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);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.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;
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);
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);
}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:
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.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();
}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.








See the prices for this post in Mr.Bool Credits System below: