Need Help Editing My Code Please!! (DigitalClock.java GUI) Here are the assignme
ID: 3806364 • Letter: N
Question
Need Help Editing My Code Please!! (DigitalClock.java GUI)
Here are the assignment details;
Set the initial size of the scene to 200 x 200. This will make the following position computation easier.
Add a circle object into the center of the pane. Set the parameters appropriately according to the initial size of the pane.
Add the four numbers: 12, 3, 6, and 9 as Text objects into the pane. Again, compute and test the parameters to position them appropriately.
Add three lines, representing the hour, minute and second hands respectively. Use different color and length to distinguish them.
Here is what I did so far (My numbers are all off);
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
public class DisplayClock extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane to hold the clock components
Pane pane = new Pane();
//Your code here: create a circle, and it to your pane
Circle c1 = new Circle(0, 0, 100);
// c1.setCenterX(0);
// c1.setCenterY(0);
// c1.setRadius(190);
c1.centerXProperty().bind(pane.widthProperty().divide(2));
c1.centerYProperty().bind(pane.heightProperty().divide(2));
c1.setRadius(100);
c1.setStroke(Color.BLACK);
c1.setFill(Color.WHITE);
pane.getChildren().add(c1);
//Your code here: create 4 texts, representing the four numbers on your clock.
Text text1 = new Text(70, 70, "12");
pane.getChildren().add(text1);
Text text2 = new Text(90, 90, "3");
pane.getChildren().add(text2);
Text text3 = new Text(160, 160, "6");
pane.getChildren().add(text3);
Text text4 = new Text(300, 300, "9");
pane.getChildren().add(text4);
//Your code here: create 3 lines, representing the hour, minute and second on your clock.
class LinePane extends Pane {
public LinePane() {
Line line1 = new Line(190, 190, 190, 190);
line1.endXProperty().bind(widthProperty().subtract(10));
line1.endYProperty().bind(heightProperty().subtract(10));
line1.setStrokeWidth(5);
line1.setStroke(Color.BLUE);
getChildren().add(line1);
Line line2 = new Line(10, 20, 10, 10);
line2.startXProperty().bind(widthProperty().subtract(10));
line2.endYProperty().bind(heightProperty().subtract(10));
line2.setStrokeWidth(5);
line2.setStroke(Color.GREEN);
getChildren().add(line2);
Line line3 = new Line(10, 10, 40, 10);
line3.startXProperty().bind(widthProperty().subtract(10));
line3.endYProperty().bind(heightProperty().subtract(10));
line3.setStrokeWidth(5);
line3.setStroke(Color.RED);
getChildren().add(line3);
}
}
// Create a scene and place it in the stage, modify and set the size of the initial scene to 200 by 200
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("DisplayClock"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
public class DisplayClock extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane to hold the clock components
Pane pane = new Pane();
//Your code here: create a circle, and it to your pane
Circle c1 = new Circle(0, 0, 100);
// c1.setCenterX(0);
// c1.setCenterY(0);
// c1.setRadius(190);
c1.centerXProperty().bind(pane.widthProperty().divide(2));
c1.centerYProperty().bind(pane.heightProperty().divide(2));
c1.setRadius(100);
c1.setStroke(Color.BLACK);
c1.setFill(Color.WHITE);
pane.getChildren().add(c1);
//Your code here: create 4 texts, representing the four numbers on your clock.
Text text1 = new Text(70, 70, "12");
pane.getChildren().add(text1);
Text text2 = new Text(90, 90, "3");
pane.getChildren().add(text2);
Text text3 = new Text(160, 160, "6");
pane.getChildren().add(text3);
Text text4 = new Text(300, 300, "9");
pane.getChildren().add(text4);
//Your code here: create 3 lines, representing the hour, minute and second on your clock.
class LinePane extends Pane {
public LinePane() {
Line line1 = new Line(190, 190, 190, 190);
line1.endXProperty().bind(widthProperty().subtract(10));
line1.endYProperty().bind(heightProperty().subtract(10));
line1.setStrokeWidth(5);
line1.setStroke(Color.BLUE);
getChildren().add(line1);
Line line2 = new Line(10, 20, 10, 10);
line2.startXProperty().bind(widthProperty().subtract(10));
line2.endYProperty().bind(heightProperty().subtract(10));
line2.setStrokeWidth(5);
line2.setStroke(Color.GREEN);
getChildren().add(line2);
Line line3 = new Line(10, 10, 40, 10);
line3.startXProperty().bind(widthProperty().subtract(10));
line3.endYProperty().bind(heightProperty().subtract(10));
line3.setStrokeWidth(5);
line3.setStroke(Color.RED);
getChildren().add(line3);
}
}
// Create a scene and place it in the stage, modify and set the size of the initial scene to 200 by 200
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("DisplayClock"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.