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.

**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.

/*Java Program to Display a Clock using Applet*/ import java.applet.*; import java.awt.*; import java.util.*; public class Clock extends Applet implements Runnable { Thread t; //Initialize the applet public void init() { setBackground(Color.white); } //Function to start the thread public void start() { t = new Thread(this); t.start(); } //Function to execute the thread public void run() { while(true) { try { repaint(); //Delay by 1 sec Thread.sleep(1000); } catch(Exception e) { } } } //Function to draw the clock public void paint(Graphics g) { Calendar time = Calendar.getInstance(); int hour = time.get(Calendar.HOUR_OF_DAY) % 12; int minute = time.get(Calendar.MINUTE); int second = time.get(Calendar.SECOND); double angle; int x,y; //Draw a circle with center(250,250) & radius=150 g.drawOval(100,100,300,300); //Label the clock String s="12"; int i=0; while(i<12) { angle = Math.toRadians(30*(i-3)); x = 250+(int)(Math.cos(angle)*135); y = 250+(int)(Math.sin(angle)*135); g.drawString(s,x,y); i++; s=String.valueOf(i); } //Draw the hours hand g.setColor(Color.green); angle = Math.toRadians((30*hour)-90); x = 250+(int)(Math.cos(angle)*100); y = 250+(int)(Math.sin(angle)*100); g.drawLine(250,250,x,y); //Draw the minutes hand g.setColor(Color.red); angle = Math.toRadians((6*minute)-90); x = 250+(int)(Math.cos(angle)*115); y = 250+(int)(Math.sin(angle)*115); g.drawLine(250,250,x,y); //Draw the seconds hand g.setColor(Color.blue); angle = Math.toRadians((6*second)-90); x = 250+(int)(Math.cos(angle)*130); y = 250+(int)(Math.sin(angle)*130); g.drawLine(250,250,x,y); } } /* <applet code = Clock.class width=500 height=500> </applet> */

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θ .**

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)

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

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

[ad_2]