luliu

HW #15 Set Clock Time App

Apr 15th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 4/15/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  *
  7.  * Assignment: HW # 15
  8.  * Programe Description:
  9.  * Set Clock Time APP
  10.  */
  11.  
  12.  
  13.  
  14. package clockAnimation;
  15.  
  16. import java.util.InputMismatchException;
  17.  
  18. import clockAnimation.ClockAnimate;
  19. import javafx.application.Application;
  20. import javafx.stage.Stage;
  21. import javafx.animation.KeyFrame;
  22. import javafx.animation.Timeline;
  23. import javafx.event.ActionEvent;
  24. import javafx.event.EventHandler;
  25. import javafx.geometry.Insets;
  26. import javafx.geometry.Pos;
  27. import javafx.scene.Scene;
  28. import javafx.scene.control.Button;
  29. import javafx.scene.control.Label;
  30. import javafx.scene.control.TextField;
  31. import javafx.scene.control.ToggleGroup;
  32. import javafx.scene.input.KeyCode;
  33. import javafx.scene.layout.BorderPane;
  34. import javafx.scene.layout.HBox;
  35. import javafx.scene.layout.Pane;
  36. import javafx.util.Duration;
  37.  
  38. public class ClockAnimationApp extends Application {
  39.     @Override // Override the start method in the Application class
  40.     public void start(Stage primaryStage) {
  41.         ClockAnimate clock = new ClockAnimate(); // Create a clock
  42.  
  43.         // Create the top buttons;
  44.         HBox paneForBottons = new HBox();
  45.         paneForBottons.setSpacing(15);
  46.         paneForBottons.setAlignment(Pos.CENTER);
  47.         Button btStart = new Button("Start");
  48.         Button btSop = new Button("Stop");
  49.         Button btLoadStart = new Button("Load Current Time & Start");
  50.         Button btLoad = new Button("Just Load Current Time");
  51.         paneForBottons.getChildren().addAll(btStart, btSop, btLoadStart, btLoad);
  52.  
  53.         // Create the bottom textField;
  54.         HBox paneForTextField = new HBox();
  55.         paneForTextField.setPadding(new Insets(10, 5, 5, 5));
  56.         paneForTextField.setSpacing(15);
  57.         paneForTextField.setStyle("-fx-border-color: green");
  58.         TextField tfHour = new TextField();
  59.         TextField tfMinute = new TextField();
  60.         TextField tfSecond = new TextField();
  61.         paneForTextField.getChildren().addAll(new Label("Hour: "), tfHour, new Label("Minute: "), tfMinute,
  62.                 new Label("Second: "), tfSecond);
  63.  
  64.         // Set the keyEvent and catch exception;
  65.         paneForTextField.setOnKeyPressed(e -> {
  66.             if (e.getCode() == KeyCode.ENTER) {
  67.                 try {
  68.                     int hour = Integer.parseInt(tfHour.getText());
  69.                     int min = Integer.parseInt(tfMinute.getText());
  70.                     int sec = Integer.parseInt(tfSecond.getText());
  71.                     if (hour <= 23 && hour >= 0 && min <= 59 && min >= 0 && sec <= 59 && sec >= 0) {
  72.                         clock.setHour(hour);
  73.                         clock.setMinute(min);
  74.                         clock.setSecond(sec);
  75.                     } else
  76.                         System.out.println("Please input rigt range integer: \nHour: 0-23\nMinute: 0-59\nSecond: 0-59");
  77.                 } catch (Exception ex) {
  78.                     System.out.println("Invalid Input");
  79.                 }
  80.             }
  81.         });
  82.  
  83.         // Create eventHandler for update the clock;
  84.         EventHandler<ActionEvent> eventHandler = e -> {
  85.             clock.setCurrentTime();
  86.             tfUpdate(clock, tfHour, tfMinute, tfSecond);
  87.         };
  88.  
  89.         // Create the animation for running clock;
  90.         Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
  91.         animation.setCycleCount(Timeline.INDEFINITE);
  92.         btLoad.setOnAction(e -> {
  93.             clock.setCurrentTime();
  94.             tfUpdate(clock, tfHour, tfMinute, tfSecond);
  95.         });
  96.         btStart.setOnAction(e -> animation.play());
  97.         btSop.setOnAction(e -> animation.pause());
  98.         btLoadStart.setOnAction(e -> animation.play());
  99.  
  100.         // Create a borderPane;
  101.         BorderPane borderPane = new BorderPane();
  102.         borderPane.setTop(paneForBottons);
  103.         borderPane.setCenter(clock);
  104.         borderPane.setBottom(paneForTextField);
  105.         BorderPane.setAlignment(paneForBottons, Pos.CENTER);
  106.         BorderPane.setAlignment(paneForTextField, Pos.CENTER);
  107.  
  108.         // Create a scene and place it in the stage
  109.         Scene scene = new Scene(borderPane, 700, 450);
  110.         tfHour.requestFocus();
  111.         primaryStage.setTitle("Clock  Animation  App");
  112.         primaryStage.setScene(scene);
  113.         primaryStage.show();
  114.     }
  115.  
  116.     /**
  117.      * The main method is only needed for the IDE with limited JavaFX support.
  118.      * Not needed for running from the command line.
  119.      */
  120.     public static void main(String[] args) {
  121.         launch(args);
  122.     }
  123.  
  124.     // Method tfUpdate;
  125.     public void tfUpdate(ClockAnimate clock, TextField tfHour, TextField tfMinute, TextField tfSecond) {
  126.         tfHour.setText(Integer.toString(clock.getHour()));
  127.         tfMinute.setText(Integer.toString(clock.getMinute()));
  128.         tfSecond.setText(Integer.toString(clock.getSecond()));
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment