Question: Task1: Display a Clock Your task is to use the shapes we introduced today to create a clock display as shown by the above example.
Task1: Display a Clock
Your task is to use the shapes we introduced today to create a clock display as shown by the above example. You can pick a random time to display, or the currect time.
Download the DisplayClock.java, which has already add the basic framework you need. Do the following step by step:
1. Set the initial size of the scene to 200 x 200. This will make the following position computation easier.
2. Add a circle object into the center of the pane. Set the parameters appropriately according to the initial size of the pane.
3. 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.
4. Add three lines, representing the hour, minute and second hands respectively. Use different color and length to distinguish them.
5. (Optional) Add another text at the bottom of your clock to show the time in digits.
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
//Your code here: create 4 texts, representing the four numbers on your clock.
//Your code here: create 3 lines, representing the hour, minute and second on your clock.
// 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); 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); } }
===========
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.stage.Stage;
public class ShowBorderPane extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a border pane BorderPane pane = new BorderPane();
// Place nodes in the pane pane.setTop(new CustomPane("Top")); pane.setRight(new CustomPane("Right")); pane.setBottom(new CustomPane("Bottom")); pane.setLeft(new CustomPane("Left")); pane.setCenter(new CustomPane("Center")); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("ShowBorderPane"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage }
}
==========
import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.FlowPane; import javafx.stage.Stage;
public class ShowFlowPane extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane and set its properties FlowPane pane = new FlowPane(); pane.setPadding(new Insets(11, 12, 13, 14)); pane.setHgap(5); pane.setVgap(5);
// Place nodes in the pane pane.getChildren().addAll(new Label("First Name:"), new TextField(), new Label("MI:")); TextField tfMi = new TextField(); tfMi.setPrefColumnCount(1); pane.getChildren().addAll(tfMi, new Label("Last Name:"), new TextField()); // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 250); primaryStage.setTitle("ShowFlowPane"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
============
import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage;
public class ShowGridPane extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane and set its properties GridPane pane = new GridPane(); pane.setAlignment(Pos.CENTER); pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5)); pane.setHgap(5.5); pane.setVgap(5.5); // Place nodes in the pane pane.add(new Label("First Name:"), 0, 0); pane.add(new TextField(), 1, 0); pane.add(new Label("MI:"), 0, 1); pane.add(new TextField(), 1, 1); pane.add(new Label("Last Name:"), 0, 2); pane.add(new TextField(), 1, 2); Button btAdd = new Button("Add Name"); pane.add(btAdd, 1, 3); GridPane.setHalignment(btAdd, HPos.RIGHT); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("ShowGridPane"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
==========
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.image.ImageView;
public class ShowHBoxVBox extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a border pane BorderPane pane = new BorderPane();
// Place nodes in the pane pane.setTop(getHBox()); pane.setLeft(getVBox()); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("ShowHBoxVBox"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } private HBox getHBox() { HBox hBox = new HBox(15); hBox.setPadding(new Insets(15, 15, 15, 15)); hBox.setStyle("-fx-background-color: gold"); hBox.getChildren().add(new Button("Computer Science")); hBox.getChildren().add(new Button("Chemistry")); ImageView imageView = new ImageView(new Image("image/us.gif")); hBox.getChildren().add(imageView); return hBox; } private VBox getVBox() { VBox vBox = new VBox(15); vBox.setPadding(new Insets(15, 5, 5, 5)); vBox.getChildren().add(new Label("Courses")); Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"), new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course: courses) { VBox.setMargin(course, new Insets(0, 0, 0, 15)); vBox.getChildren().add(course); } return vBox; } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
===========
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Line;
public class ShowLine extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place it in the stage Scene scene = new Scene(new LinePane(), 200, 200); primaryStage.setTitle("ShowLine"); // 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); } }
class LinePane extends Pane { public LinePane() { Line line1 = new Line(10, 10, 10, 10); line1.endXProperty().bind(widthProperty().subtract(10)); line1.endYProperty().bind(heightProperty().subtract(10)); line1.setStrokeWidth(5); line1.setStroke(Color.GREEN); getChildren().add(line1); Line line2 = new Line(10, 10, 10, 10); line2.startXProperty().bind(widthProperty().subtract(10)); line2.endYProperty().bind(heightProperty().subtract(10)); line2.setStrokeWidth(5); line2.setStroke(Color.GREEN); getChildren().add(line2); } }
=============
import java.util.Collections;
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Text; import javafx.scene.shape.Rectangle;
public class ShowRectangle extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane Pane pane = new Pane(); // Create rectangles and add to pane Rectangle r1 = new Rectangle(25, 10, 60, 30); r1.setStroke(Color.BLACK); r1.setFill(Color.WHITE); pane.getChildren().add(new Text(10, 27, "r1")); pane.getChildren().add(r1); Rectangle r2 = new Rectangle(25, 50, 60, 30); pane.getChildren().add(new Text(10, 67, "r2")); pane.getChildren().add(r2); Rectangle r3 = new Rectangle(25, 90, 60, 30); r3.setArcWidth(15); r3.setArcHeight(25); pane.getChildren().add(new Text(10, 107, "r3")); pane.getChildren().add(r3); for (int i = 0; i < 4; i++) { Rectangle r = new Rectangle(100, 50, 100, 30); r.setRotate(i * 360 / 8); r.setStroke(Color.color(Math.random(), Math.random(), Math.random())); r.setFill(Color.WHITE); pane.getChildren().add(r); } // Create a scene and place it in the stage Scene scene = new Scene(pane, 250, 150); primaryStage.setTitle("ShowRectangle"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
============
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; 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 ShowText extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane to hold the texts Pane pane = new Pane(); pane.setPadding(new Insets(5, 5, 5, 5)); Text text1 = new Text(20, 20, "Programming is fun"); text1.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 15)); pane.getChildren().add(text1);
Text text2 = new Text(60, 60, "Programming is fun Display text"); pane.getChildren().add(text2);
Text text3 = new Text(10, 100, "Programming is fun Display text"); text3.setFill(Color.RED); text3.setUnderline(true); text3.setStrikethrough(true); pane.getChildren().add(text3); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("ShowText"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
