Hey Alien, welcome to practice house. in this post we will learn how to create an analog clock in java using java applet. This program will help you understand about a logic that clock use and you will learn about thread. It’s just a fun project you can do and show to your friend that you did it.
Customize a little bit and have fun. So here is the code
File:AnalaogClock.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package com.practice.house; import java.util.*; import java.text.*; import java.applet.*; import java.awt.*; public class AnalogClock extends Applet implements Runnable { String clkString = ""; int clockHours = 0, clockMinutes = 0, clockSeconds = 0; int clkwidth, clkheight; boolean threadSuspended; Thread thr = null; public void init() { clkwidth = getSize().width; clkheight = getSize().height; setBackground(Color.cyan); } public void start() { if (thr == null) { thr = new Thread(this); thr.setPriority(Thread.MIN_PRIORITY); threadSuspended = false; thr.start(); }else { if (threadSuspended) { threadSuspended = false; synchronized (this) { notify(); } } } } public void stop() { threadSuspended = true; } public void run() { try { while (true) { Calendar calender = Calendar.getInstance(); clockHours = calender.get(Calendar.HOUR_OF_DAY); if (clockHours > 12) clockHours -= 12; clockMinutes = calender.get(Calendar.MINUTE); clockSeconds = calender.get(Calendar.SECOND); SimpleDateFormat frmatter = new SimpleDateFormat("hh:mm:ss", Locale.getDefault()); Date d = calender.getTime(); clkString = frmatter.format(d); if (threadSuspended) { synchronized (this) { while (threadSuspended) { wait(); } } } repaint(); thr.sleep(1000); } } catch (Exception e) { System.out.printf("","Error Occured"); } } void drawHand(double angle, int radius, Graphics grp) { angle -= 0.5 * Math.PI; int a = (int) (radius * Math.cos(angle)); int b = (int) (radius * Math.sin(angle)); grp.drawLine(clkwidth / 2, clkheight / 2, clkwidth / 2 + a, clkheight / 2 + b); } void drawWedge(double angle, int radius, Graphics grp) { angle -= 0.5 * Math.PI; int a = (int) (radius * Math.cos(angle)); int b = (int) (radius * Math.sin(angle)); angle += 2 * Math.PI / 3; int a2 = (int) (5 * Math.cos(angle)); int b2 = (int) (5 * Math.sin(angle)); angle += 2 * Math.PI / 3; int a3 = (int) (5 * Math.cos(angle)); int b3 = (int) (5 * Math.sin(angle)); grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a, clkheight / 2 + b); grp.drawLine(clkwidth / 2 + a3, clkheight / 2 + b3, clkwidth / 2 + a, clkheight / 2 + b); grp.drawLine(clkwidth / 2 + a2, clkheight / 2 + b2, clkwidth / 2 + a3, clkheight / 2 + b3); } public void paint(Graphics grp) { grp.setColor(Color.red); drawWedge(2 * Math.PI * clockHours / 12, clkwidth / 5, grp); drawWedge(2 * Math.PI * clockMinutes / 60, clkwidth / 3, grp); drawHand(2 * Math.PI * clockSeconds / 60, clkwidth / 2, grp); grp.setColor(Color.black); grp.drawString(clkString, 10, clkheight - 10); grp.drawString("pracitceHouse.com", 113, 300); } } |
hey, so it’s kinda big program but very easy to understand that what’s going on this code. i hope you are having fun after running this application. let me share how my clock looks like.
OUTPUT:
okay guys, see you in another post. here in practice house we have a log of example java programs that will help you to learn a lot of programs. so stay tune with us.
HAPPY CODING … HAPPY LIVING

Java – PHP – Python – Dart – Flutter – MySql – Spring – Hibernate – JavaScript – jQuery – BootStrap 4 – CSS – HTML
I love to explore new technologies. If you like my tutorials please share your thoughts about my work.
Check out my YouTube Channel For Video Tutorials.
To Hire Me Please Contact Me Through Social Media or https:www.amialif.com
If you want to help others by doing donation please Help Them.
You also can connect with me via social media. Thanks
Leave a Reply