commit 27852366945cf32c40aa39a28576dc27870ecc5a Author: lukas-heiligenbrunner Date: Fri Apr 10 18:26:03 2020 +0200 init diff --git a/src/sample/Controller.java b/src/sample/Controller.java new file mode 100644 index 0000000..4f65434 --- /dev/null +++ b/src/sample/Controller.java @@ -0,0 +1,122 @@ +package sample; + +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + +import java.text.DecimalFormat; +import java.util.Date; + +public class Controller { + public Button btnstart; + public Button btnpause; + public Button btnstop; + public Label timelabel; + public Button btnresume; + public TextField textinput; + public TextField entimefield; + + private long starttime = new Date().getTime(); + private long endtime; + private long warningtime; + + private Thread curr = null; + private long pausetime = new Date().getTime(); + private DecimalFormat format = new DecimalFormat("00"); + + private final Runnable timer = new Runnable() { + @Override + public void run() { + while (true) { + long currtime = new Date().getTime() - starttime; + + int sec = (int) currtime / 1000; + int min = (int) currtime / (1000 * 60); + int ms = (int) currtime - (sec * 1000 + min * (1000 * 60)); + Platform.runLater(() -> timelabel.setText(format.format(min) + ":" + format.format(sec) + ":" + format.format((int) ((float) ms / 10) + 0.5))); + + if (warningtime < currtime) { + timelabel.setStyle("-fx-background-color: #FF0000"); + } + + if (endtime < currtime) { + btnstart.setDisable(false); + btnstop.setDisable(true); + + curr.stop(); + } + + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }; + + public Controller() { + Platform.runLater(() -> { + btnstop.setDisable(true); + btnpause.setDisable(true); + btnresume.setDisable(true); + }); + } + + public void startclick(ActionEvent actionEvent) { + btnstart.setDisable(true); + btnpause.setDisable(false); + btnstop.setDisable(false); + + starttime = new Date().getTime(); + + // parse warning field + String inp = textinput.getText(); + int mins = Integer.parseInt(inp.substring(0, 2)); + int secs = Integer.parseInt(inp.substring(3, 5)); + int mss = Integer.parseInt(inp.substring(6, 8)); + warningtime = mss + secs * 1000 + mins * 60 * 1000; + + // parse end field + String inpe = entimefield.getText(); + int mine = Integer.parseInt(inpe.substring(0, 2)); + int sece = Integer.parseInt(inpe.substring(3, 5)); + int mse = Integer.parseInt(inpe.substring(6, 8)); + endtime = mse + sece * 1000 + mine * 60 * 1000; + + timelabel.setStyle("-fx-background-color: #ebebeb"); + + curr = new Thread(timer); + curr.start(); + } + + public void pauseclick(ActionEvent actionEvent) { + btnpause.setDisable(true); + btnresume.setDisable(false); + + pausetime = new Date().getTime(); + curr.stop(); + } + + public void stopclick(ActionEvent actionEvent) { + btnstart.setDisable(false); + btnstop.setDisable(true); + btnpause.setDisable(true); + btnresume.setDisable(true); + + curr.stop(); + } + + public void resumeclick(ActionEvent actionEvent) { + btnresume.setDisable(true); + btnpause.setDisable(false); + + starttime += (new Date().getTime() - pausetime); + + curr = new Thread(timer); + curr.start(); + } +} \ No newline at end of file diff --git a/src/sample/Main.java b/src/sample/Main.java new file mode 100644 index 0000000..57e6d1c --- /dev/null +++ b/src/sample/Main.java @@ -0,0 +1,24 @@ +package sample; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) throws Exception{ + Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); + primaryStage.setTitle("Stoppuhr"); + primaryStage.setResizable(false); + primaryStage.setScene(new Scene(root, 300, 500)); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/src/sample/sample.fxml b/src/sample/sample.fxml new file mode 100644 index 0000000..02941ae --- /dev/null +++ b/src/sample/sample.fxml @@ -0,0 +1,22 @@ + + + + + + + + +