Java Reference
In-Depth Information
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:
secondX = centerX + secondHandLength × sin(second × (2π/60))
secondY = centerY - secondHandLength × cos(second × (2π/60))
minuteX = centerX + minuteHandLength × sin(minute × (2π/60))
minuteY = centerY - minuteHandLength × cos(minute × (2π/60))
hourX = centerX + hourHandLength × sin((hour + minute/60) × (2π/12))
hourY = centerY - hourHandLength × cos((hour + minute/60) × (2π/12))
The ClockPane class is implemented in Listing 14.21.
L ISTING 14.21
ClockPane.java
1 import java.util.Calendar;
2 import java.util.GregorianCalendar;
3 import javafx.scene.layout.Pane;
4 import javafx.scene.paint.Color;
5 import javafx.scene.shape.Circle;
6 import javafx.scene.shape.Line;
7 import javafx.scene.text.Text;
8
9 public class ClockPane extends Pane {
10
private int hour;
clock properties
11
private int minute;
12
private int second;
13
14
// Clock pane's width and height
15
private double w = 250 , h = 250 ;
16
17 /** Construct a default clock with the current time*/
18 public ClockPane() {
19 setCurrentTime();
20 }
21
22 /** Construct a clock with specified hour, minute, and second */
23 public ClockPane( int hour, int minute, int second) {
24 this .hour = hour;
25 this .minute = minute;
26 this .second = second;
27 paintClock();
28 }
29
30
no-arg constructor
constructor
/** Return hour */
31
public int getHour() {
32
return hour;
33 }
34
35 /** Set a new hour */
36 public void setHour( int hour) {
37 this .hour = hour;
38 paintClock();
39 }
40
41
set a new hour
paint clock
/** Return minute */
42
public int getMinute() {
43
return minute;
44 }
45
 
 
Search WWH ::




Custom Search