Java Program to Display a Clock Using Applet

«
»

This is a Java Program to Display a Clock Using Applet

Problem Description

We have to write a program in Java such that it creates an analog clock which displays the current system time.

Expected Input and Output

For displaying a clock, we can have the following set of input and output.

advertisement

To View the Clock :

When the program is executed,
it is expected that the applet consists of a clock,
and the clock displays the current system time.
Problem Solution

1. Create a thread and delay each execution by 1 seconds.
2. Get the hour, minute and second parameters from the system time.
3. Create the clock and label the hours on it.
4. Draw the hours hand, minutes hand and the seconds hand based on the current system time.

Program/Source Code

Here is source code of the Java program to display a clock. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

advertisement
  1. /*Java Program to Display a Clock using Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.util.*;
  5. public class Clock extends Applet implements Runnable
  6. {
  7.     Thread t;
  8.     //Initialize the applet
  9.     public void init()
  10.     {
  11. 	setBackground(Color.white);
  12.     }
  13.     //Function to start the thread
  14.     public void start()
  15.     {
  16. 	t = new Thread(this);
  17. 	t.start();
  18.     }
  19.     //Function to execute the thread
  20.     public void run()
  21.     {
  22. 	while(true)
  23. 	{
  24. 	    try
  25. 	    {
  26. 		repaint();
  27. 		//Delay by 1 sec
  28. 		Thread.sleep(1000);
  29. 	    }
  30. 	    catch(Exception e)
  31.             {
  32.             }
  33. 	}
  34.     }
  35.     //Function to draw the clock
  36.     public void paint(Graphics g)
  37.     {
  38. 	Calendar time = Calendar.getInstance();
  39. 	int hour = time.get(Calendar.HOUR_OF_DAY) % 12;
  40. 	int minute = time.get(Calendar.MINUTE);
  41. 	int second = time.get(Calendar.SECOND);
  42. 	double angle;
  43. 	int x,y;
  44. 	//Draw a circle with center(250,250) & radius=150
  45. 	g.drawOval(100,100,300,300);
  46. 	//Label the clock
  47. 	String s="12";
  48. 	int i=0;
  49. 	while(i<12)
  50. 	{
  51. 	    angle = Math.toRadians(30*(i-3));
  52. 	    x = 250+(int)(Math.cos(angle)*135);
  53. 	    y = 250+(int)(Math.sin(angle)*135);
  54. 	    g.drawString(s,x,y);
  55. 	    i++;
  56. 	    s=String.valueOf(i);
  57. 	}	
  58. 	//Draw the hours hand
  59. 	g.setColor(Color.green);
  60. 	angle = Math.toRadians((30*hour)-90);
  61. 	x = 250+(int)(Math.cos(angle)*100);
  62. 	y = 250+(int)(Math.sin(angle)*100);
  63.         g.drawLine(250,250,x,y);
  64. 	//Draw the minutes hand
  65. 	g.setColor(Color.red);
  66. 	angle = Math.toRadians((6*minute)-90);
  67. 	x = 250+(int)(Math.cos(angle)*115);
  68. 	y = 250+(int)(Math.sin(angle)*115);
  69. 	g.drawLine(250,250,x,y);
  70. 	//Draw the seconds hand
  71. 	g.setColor(Color.blue);
  72. 	angle = Math.toRadians((6*second)-90);
  73. 	x = 250+(int)(Math.cos(angle)*130);
  74. 	y = 250+(int)(Math.sin(angle)*130);
  75. 	g.drawLine(250,250,x,y);
  76.     }
  77. }
  78. /*
  79. <applet code = Clock.class width=500 height=500>
  80. </applet>
  81. */

To compile and run the program use the following commands :

>>>javac Clock.java
>>>appletviewer Clock.java
Program Explanation

1. Use Thread.sleep(int) to delay the execution by entered time in milliseconds.
2. Create a circle for the clock and label the hours on it.
3. Each hour on the clock is equivalent to 30 degrees. The angle of hours hand from the y=0 line, is ((30*hour)-90) degrees.
4. Each minute on the clock is equivalent to 6 degrees. The angle of minutes hand from the y=0 line, is ((6*minute)-90) degrees.
5. Each second on the clock is equivalent to 6 degrees. The angle of seconds hand from the y=0 line, is ((6*second)-90) degrees.
6. To get the position of the hands of clock, it is required to convert the angles to radians. To do this, use Math.toRadians function.
7. The x co-ordinate of any point on a circle with center (a,b) and radius ‘r’ measuring an angle θ from the center is given as x = a + r * cosθ .
8. The y co-ordinate of any point on a circle with center (a,b) and radius ‘r’ measuring an angle θ from the center is given as y = v + r * sinθ .

advertisement
Runtime Test Cases

Here’s the run time test cases for creating a clock for different input cases.

Test case 1 – To View the Clock. (When time is 18:10:06)
java-applet-clock-1

Test case 2 – To View the Clock. (When time is 18:12:23)
java-applet-clock-2

Test case 3 – To View the Clock. (When time is 18:14:35)
java-applet-clock-3

advertisement

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn