Java Reference
In-Depth Information
The position of the minute hand is determined by the minute and second. The exact minute
value combined with seconds is minute + second/60 . For example, if the time is 3 min-
utes and 30 seconds, the total minutes are 3.5. Since there are 60 minutes in one hour, the
angle for the minute hand is
(minute + second/60) × (2 π /60)
Since one circle is divided into 12 hours, the angle for the hour hand is
(hour + minute/60 + second/(60 × 60)) × (2 π /12)
For simplicity in computing the angles of the minute hand and hour hand, you can omit the
seconds, because they are negligibly small. Therefore, the endpoints for the second hand,
minute hand, and hour hand can be computed as:
xSecond = xCenter + secondHandLength × sin(second × (2 π /60))
ySecond = yCenter - secondHandLength × cos(second × (2 π /60))
xMinute = xCenter + minuteHandLength × sin(minute × (2 π /60))
yMinute = yCenter - minuteHandLength × cos(minute × (2 π /60))
xHour = xCenter + hourHandLength × sin((hour + minute/60) × (2 π /12))
yHour = yCenter - hourHandLength × cos((hour + minute/60) × (2 π /12))
The StillClock class is implemented in Listing 13.10.
L ISTING 13.10 StillClock.java
1 import java.awt.*;
2 import javax.swing.*;
3 import java.util.*;
4
5 public class StillClock extends JPanel {
6
private int hour;
7
private int minute;
8
private int second;
9
10
/** Construct a default clock with the current time*/
11
public StillClock()
{
12 setCurrentTime();
13 }
14
15
/** Construct a clock with specified hour, minute, and second */
16
public StillClock( int hour, int minute, int second)
{
17
this .hour = hour;
18
this .minute = minute;
19
this .second = second;
20 }
21
22
/** Return hour */
23
public int getHour() {
24
return hour;
25 }
26
27 /** Set a new hour */
28 public void setHour( int hour) {
29 this .hour = hour;
30 repaint();
31 }
32
33
repaint panel
/** Return minute */
 
Search WWH ::




Custom Search