Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Lu Liu
- * Date: 4/15/2016
- * Course Number: CSC-112
- * Course Name: Intermediate Topics in Java Programming
- * Email: lliu0001@student.stcc.edu
- *
- * Assignment: HW # 15
- * Programe Description:
- * Set Clock Time APP
- */
- package clockAnimation;
- import java.util.InputMismatchException;
- import clockAnimation.ClockAnimate;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- 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.control.ToggleGroup;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.util.Duration;
- public class ClockAnimationApp extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- ClockAnimate clock = new ClockAnimate(); // Create a clock
- // Create the top buttons;
- HBox paneForBottons = new HBox();
- paneForBottons.setSpacing(15);
- paneForBottons.setAlignment(Pos.CENTER);
- Button btStart = new Button("Start");
- Button btSop = new Button("Stop");
- Button btLoadStart = new Button("Load Current Time & Start");
- Button btLoad = new Button("Just Load Current Time");
- paneForBottons.getChildren().addAll(btStart, btSop, btLoadStart, btLoad);
- // Create the bottom textField;
- HBox paneForTextField = new HBox();
- paneForTextField.setPadding(new Insets(10, 5, 5, 5));
- paneForTextField.setSpacing(15);
- paneForTextField.setStyle("-fx-border-color: green");
- TextField tfHour = new TextField();
- TextField tfMinute = new TextField();
- TextField tfSecond = new TextField();
- paneForTextField.getChildren().addAll(new Label("Hour: "), tfHour, new Label("Minute: "), tfMinute,
- new Label("Second: "), tfSecond);
- // Set the keyEvent and catch exception;
- paneForTextField.setOnKeyPressed(e -> {
- if (e.getCode() == KeyCode.ENTER) {
- try {
- int hour = Integer.parseInt(tfHour.getText());
- int min = Integer.parseInt(tfMinute.getText());
- int sec = Integer.parseInt(tfSecond.getText());
- if (hour <= 23 && hour >= 0 && min <= 59 && min >= 0 && sec <= 59 && sec >= 0) {
- clock.setHour(hour);
- clock.setMinute(min);
- clock.setSecond(sec);
- } else
- System.out.println("Please input rigt range integer: \nHour: 0-23\nMinute: 0-59\nSecond: 0-59");
- } catch (Exception ex) {
- System.out.println("Invalid Input");
- }
- }
- });
- // Create eventHandler for update the clock;
- EventHandler<ActionEvent> eventHandler = e -> {
- clock.setCurrentTime();
- tfUpdate(clock, tfHour, tfMinute, tfSecond);
- };
- // Create the animation for running clock;
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- btLoad.setOnAction(e -> {
- clock.setCurrentTime();
- tfUpdate(clock, tfHour, tfMinute, tfSecond);
- });
- btStart.setOnAction(e -> animation.play());
- btSop.setOnAction(e -> animation.pause());
- btLoadStart.setOnAction(e -> animation.play());
- // Create a borderPane;
- BorderPane borderPane = new BorderPane();
- borderPane.setTop(paneForBottons);
- borderPane.setCenter(clock);
- borderPane.setBottom(paneForTextField);
- BorderPane.setAlignment(paneForBottons, Pos.CENTER);
- BorderPane.setAlignment(paneForTextField, Pos.CENTER);
- // Create a scene and place it in the stage
- Scene scene = new Scene(borderPane, 700, 450);
- tfHour.requestFocus();
- primaryStage.setTitle("Clock Animation App");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- /**
- * 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);
- }
- // Method tfUpdate;
- public void tfUpdate(ClockAnimate clock, TextField tfHour, TextField tfMinute, TextField tfSecond) {
- tfHour.setText(Integer.toString(clock.getHour()));
- tfMinute.setText(Integer.toString(clock.getMinute()));
- tfSecond.setText(Integer.toString(clock.getSecond()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement