Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Set clock time) Write a program that displays a clock and sets the time with th

ID: 3686437 • Letter: #

Question

(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 16.38b. Use the ClockPane in Listing 14.21. Resize the clock to the center of the pane.

Figure 16.38b

The ClockPane class is implemented in Listing 14.21.
LISTING 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;
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
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 }

Exercise16-07 12 9 3 6 Hour4 Minute 30 Second 45

Explanation / Answer

Can help you iwth this any more information needed please comment


import ToolKit.ClockPane;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class clock extends Application
{
public void start(Stage primaryStage) throws Exception
{
ClockPane clockPane = new ClockPane();
TextField tfHour = new TextField(clockPane.getHour() + "");
tfHour.setPrefColumnCount(2);
Label lblHour = new Label("Hour:", tfHour);
lblHour.setContentDisplay(ContentDisplay.RIGHT);
tfHour.setOnAction(e-> clockPane.setHour(Integer.parseInt(tfHour.getText())));
TextField tfMinutes = new TextField(clockPane.getMinute() + "");
tfMinutes.setPrefColumnCount(2);
Label lblMinutes = new Label("Minute:", tfMinutes);
lblMinutes.setContentDisplay(ContentDisplay.RIGHT);
tfMinutes.setOnAction(e-> clockPane.setMinute(Integer.parseInt(tfMinutes.getText())));
TextField tfSeconds = new TextField(clockPane.getSecond() + "");
tfSeconds.setPrefColumnCount(2);
Label lblSeconds = new Label("Second:", tfSeconds);
lblSeconds.setContentDisplay(ContentDisplay.RIGHT);
tfSeconds.setOnAction(e-> clockPane.setSecond(Integer.parseInt(tfSeconds.getText())));
BorderPane pane = new BorderPane(clockPane);
BorderPane.setAlignment(clockPane, Pos.CENTER);
HBox textFieldPane = new HBox(5);
textFieldPane.setAlignment(Pos.CENTER);
textFieldPane.getChildren().addAll(lblHour, lblMinutes, lblSeconds);
textFieldPane.setPadding(new Insets(5));
pane.setBottom(textFieldPane);
primaryStage.setScene(new Scene(pane));
primaryStage.setTitle("Clock");
primaryStage.show();
clockPane.setFocusTraversable(true);
}
public static void main(String[] args) {
Application.launch(args);
}
}