Advertisement
luliu

HW #15 Set Clock Time App

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