Problem

(Set clock time) Write a program that displays a clock and sets the time with the input fr...

(Set clock time) Write a program that displays a clock and sets the time with the input from three text fields, as shown in Figure 1b. Use the ClockPane in Listing 1. Resize the clock to the center of the pane.

FIGURE 1 (a) You can set a text field’s properties for the horizontal alignment and column size dynamically. (b) The program displays the time specified in the text fields.

LISTING 1 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;

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 /** 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 /** Return minute */

42 public int getMinute() {

43 return minute;

44 }

45

46 /** Set a new minute */

47 public void setMinute(int minute) {

48 this.minute = minute;

49 paintClock();

50 }

51

52 /** Return second */

53 public int getSecond() {

54 return second;

55 }

56

57 /** Set a new second */

58 public void setSecond(int second) {

59 this.second = second;

60 paintClock();

61 }

62

63 /** Return clock pane's width */

64 public double getW() {

65 return w;

66 }

67

68 /** Set clock pane's width */

69 public void setW(double w) {

70 this.w = w;

71 paintClock();

72 }

73

74 /** Return clock pane's height */

75 public double getH() {

76 return h;

77 }

78

79 /** Set clock pane's height */

80 public void setH(double h) {

81 this.h = h;

82 paintClock();

83 }

84

85 /* Set the current time for the clock */

86 public void setCurrentTime() {

87 // Construct a calendar for the current date and time

88 Calendar calendar = new GregorianCalendar();

89

90 // Set current hour, minute and second

91 this.hour = calendar.get(Calendar.HOUR_OF_DAY);

92 this.minute = calendar.get(Calendar.MINUTE);

93 this.second = calendar.get(Calendar.SECOND);

94

95 paintClock(); // Repaint the clock

96 }

97

98 /** Paint the clock */

99 protected void paintClock() {

100 // Initialize clock parameters

101 double clockRadius = Math.min(w, h) * 0.8 * 0.5;

102 double centerX = w / 2;

103 double centerY = h / 2;

104

105 // Draw circle

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 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT