-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.java
More file actions
executable file
·76 lines (65 loc) · 2.42 KB
/
Copy pathHand.java
File metadata and controls
executable file
·76 lines (65 loc) · 2.42 KB
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
import java.awt.*;
import java.awt.geom.Line2D;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
public class Hand {
public final static double RADIANS = Clock.RADIANS;
public final static double CLOCK_CENTRE = Clock.CLOCK_CENTRE;
public final static double CENTRE = Clock.CENTRE;
public enum TimeUnit {
MILLS(ChronoField.MILLI_OF_SECOND, 0 , 0),
SECOND(ChronoField.SECOND_OF_MINUTE, 80, 0.5f, Color.RED),
MINUTE(ChronoField.MINUTE_OF_HOUR, 75, 1),
HOUR(ChronoField.HOUR_OF_AMPM, 50, 2);
public final ChronoField cf;
public final int length;
public final BasicStroke stroke;
public final Color color;
TimeUnit(ChronoField aCF, int aLength, float aWidth) {
this(aCF, aLength, aWidth, Color.BLACK);
}
TimeUnit(ChronoField aCF, int aLength, float aWidth, Color aColor) {
cf = aCF;
length = (Clock.SCALE * aLength) / (100 * 2);
stroke = new BasicStroke(aWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
color = aColor;
}
}
//hand object attributes.
private final TimeUnit unit; // ie: hour, minute, second, etc
public final double angle; // the rotation of the hand in radians from 12 o'clock
//Creates a hand.
public Hand(TimeUnit aUnit, LocalTime aTime) {
this(aUnit, aTime, null);
}
/**
* Creates a clock hand that uses angles.
* @param aUnit - unit used in the hand
* @param aTime - the value of the unit
* @param prevHand - the previous hand
*/
public Hand(TimeUnit aUnit, LocalTime aTime, Hand prevHand) {
//time-obtaining variables
unit = aUnit;
int value = aTime.get(unit.cf);
long maxValue = unit.cf.range().getMaximum() + 1;
//calculates the angle
if (prevHand == null) {
angle = (RADIANS/maxValue * value);
} else {
angle = RADIANS/maxValue * value + (prevHand.angle/maxValue);
}
}
/**
* Draws the clock hands.
*/
public void draw(Graphics2D g) {
// TODO FIXME draw the hand
Line2D.Double tick = new Line2D.Double(CLOCK_CENTRE, CLOCK_CENTRE, CLOCK_CENTRE, CLOCK_CENTRE - unit.length);
g.setColor(unit.color);
g.setStroke(unit.stroke);
g.rotate(angle, CENTRE, CENTRE);
g.draw(tick);
g.rotate(RADIANS - angle, CENTRE, CENTRE);
}
}