How to draw a Clock using StillClock in Java
StillClock class is used to displays an analog clock and uses the MessagePanel to display the hour, minute, and second in a panel. We use javax.swing.JPanel to display the StillClock.
This article helps to develop a class that displays a clock on a panel.
Steps used to draw the clock:
- To draw a clock, draw a circle and three hands for a second, minute and hour.
- One end is the center of the clock at (xCenter, yCenter) and the other end (xEnd, yEnd) is determined by,
xEndpart = yCenter + handLength X sin(0) yEndpart = yCenter + handLength X cos(0)
- The angle for the second hand(Sixty seconds in one minute),
- The angle for the minute hand is,
(minute + second/60)*(2*pi/60)
- Since, one circle is divided into twelve hours, the angle for the hour hand is,
(hour + minute/60 + second/(60*60)) * (2*pi/12)
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.