Java Reference
In-Depth Information
106 Circle circle =
new
Circle(centerX, centerY, clockRadius);
107 circle.setFill(Color.WHITE);
108 circle.setStroke(Color.BLACK);
109 Text t1 =
new
Text(centerX -
5
, centerY - clockRadius +
12
,
"
12
"
);
110 Text t2 =
new
Text(centerX - clockRadius +
3
, centerY +
5
,
"9"
);
111 Text t3 =
new
Text(centerX + clockRadius -
10
, centerY +
3
,
"3"
);
112 Text t4 =
new
Text(centerX -
3
, centerY + clockRadius -
3
,
"6"
);
113
114
// Draw second hand
115
double
sLength = clockRadius *
0.8
;
116
double
secondX = centerX + sLength *
117 Math.sin(second * (
2
* Math.PI /
60
));
118
double
secondY = centerY - sLength *
119 Math.cos(second * (
2
* Math.PI /
60
));
120 Line sLine =
new
Line(centerX, centerY, secondX, secondY);
121 sLine.setStroke(Color.RED);
122
123
// Draw minute hand
124
double
mLength = clockRadius *
0.65
;
125
double
xMinute = centerX + mLength *
126 Math.sin(minute * (
2
* Math.PI /
60
));
127
double
minuteY = centerY - mLength *
128 Math.cos(minute * (
2
* Math.PI /
60
));
129 Line mLine =
new
Line(centerX, centerY, xMinute, minuteY);
130 mLine.setStroke(Color.BLUE);
131
132
// Draw hour hand
133
double
hLength = clockRadius *
0.5
;
134
double
hourX = centerX + hLength *
135 Math.sin((hour %
12
+ minute /
60.0
) * (
2
* Math.PI /
12
));
136
double
hourY = centerY - hLength *
137 Math.cos((hour %
12
+ minute /
60.0
) * (
2
* Math.PI /
12
));
138 Line hLine =
new
Line(centerX, centerY, hourX, hourY);
139 hLine.setStroke(Color.GREEN);
140
141 getChildren().clear();
142 getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
143 }
144 }
create a circle
create texts
create second hand
create minute hand
create hour hand
clear pane
add to pane
The program displays a clock for the current time using the no-arg constructor (lines
18-20) and displays a clock for the specified hour, minute, and second using the other
constructor (lines 23-28). The current hour, minute, and second is obtained by using
the
GregorianCalendar
class (lines 86-96). The
GregorianCalendar
class in the
Java API enables you to create a
Calendar
instance for the current time using its no-
arg constructor. You can then use its methods
get(Calendar.HOUR)
,
get(Calendar
.MINUTE)
, and
get(Calendar.SECOND)
to return the hour, minute, and second from a
Calendar
object.
The class defines the properties
hour
,
minute
, and
second
to store the time represented
in the clock (lines 10-12) and uses the
w
and
h
properties to represent the width and height of
the clock pane (line 15). The initial values of
w
and
h
are set to 250. The
w
and
h
values can be
reset using the
setW
and
setH
methods (lines 69, 80). These values are used to draw a clock
in the pane in the
paintClock()
method.
The
paintClock()
method paints the clock (lines 99-143). The clock radius is propor-
tional to the width and height of the pane (line 101). A circle for the clock is created at the center
of the pane (line 106). The text for showing the hours 12, 3, 6, 9 are created in linesĀ 109-112.
Search WWH ::
Custom Search