From ca966e6c7b742c7b2ce02ee596c2b5eb7647a522 Mon Sep 17 00:00:00 2001 From: David Wu Date: Mon, 20 Mar 2017 15:33:37 +1300 Subject: [PATCH] Implement race clock -Added label to fxml racepane.fxml -Added calcTime method to calculate time elapsed -Added updateTime method in Race to update label text -Added setTimer in RaceController to update label text #story [16] #pair [fwy13, zwu18] --- .idea/.name | 1 - .idea/compiler.xml | 19 ++--------- .idea/misc.xml | 12 ++++--- .../seng302/Controllers/RaceController.java | 12 +++++++ src/main/java/seng302/Model/Race.java | 34 ++++++++++++++----- src/main/resources/scenes/racepane.fxml | 8 +++-- 6 files changed, 52 insertions(+), 34 deletions(-) delete mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 7a0b7f00..00000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -app \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 8e141d98..12dfecd9 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,27 +1,12 @@ - - - - - - - - - - - - - - - - + - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 334458a7..ab182112 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ - - - - + + + + diff --git a/src/main/java/seng302/Controllers/RaceController.java b/src/main/java/seng302/Controllers/RaceController.java index 0b8402f0..00731eac 100644 --- a/src/main/java/seng302/Controllers/RaceController.java +++ b/src/main/java/seng302/Controllers/RaceController.java @@ -2,11 +2,13 @@ package seng302.Controllers; import javafx.beans.property.ReadOnlyObjectWrapper; +import javafx.beans.property.StringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; @@ -34,6 +36,9 @@ public class RaceController extends Controller{ ResizableRaceCanvas raceMap; + @FXML + Label timer; + @FXML TableView boatInfoTable; @FXML @@ -125,5 +130,12 @@ public class RaceController extends Controller{ } + public void setTimer(String time){ + timer.setText(time); + } + + + + } diff --git a/src/main/java/seng302/Model/Race.java b/src/main/java/seng302/Model/Race.java index a0225d13..d840ffd8 100644 --- a/src/main/java/seng302/Model/Race.java +++ b/src/main/java/seng302/Model/Race.java @@ -1,6 +1,8 @@ package seng302.Model; +import javafx.application.Platform; +import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableArray; import javafx.collections.ObservableList; @@ -19,6 +21,7 @@ public abstract class Race implements Runnable { protected ArrayList legs; protected RaceController controller; protected int boatsFinished = 0; + protected long totalTimeElapsed; private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop @@ -69,6 +72,26 @@ public abstract class Race implements Runnable { } + private String calcTimer() { + long minutes; + long currentTimeInSeconds; + long remainingSeconds; + long hours; + + currentTimeInSeconds = totalTimeElapsed / 1000; + minutes = currentTimeInSeconds / 60; + remainingSeconds = currentTimeInSeconds % 60; + hours = minutes/60; + return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds); + } + + private void updateTime(String time){ + + Platform.runLater(() -> {controller.setTimer(time);}); + } + + + /** * Starts the Race Simulation, playing the race start to finish with the timescale. * This prints the boats participating, the order that the events occur in time order, and the respective information of the events. @@ -76,23 +99,15 @@ public abstract class Race implements Runnable { private void simulateRace() { long timeRaceStarted = System.currentTimeMillis(); - long totalTimeElapsed; long timeLoopStarted; long timeLoopEnded; - long minutes; - long currentTimeInSeconds; - long remainingSeconds; while (boatsFinished < startingBoats.size()) { timeLoopStarted = System.currentTimeMillis(); totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; - long currentTime = System.currentTimeMillis() - timeRaceStarted; - currentTimeInSeconds = currentTime / 1000; - minutes = currentTimeInSeconds / 60; - remainingSeconds = currentTimeInSeconds % 60; - System.out.println(minutes + ":" + remainingSeconds); + for (BoatInRace boat : startingBoats) { if (boat != null) { @@ -102,6 +117,7 @@ public abstract class Race implements Runnable { } controller.updateMap(startingBoats); + updateTime(calcTimer()); try { timeLoopEnded = System.currentTimeMillis(); Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted)); diff --git a/src/main/resources/scenes/racepane.fxml b/src/main/resources/scenes/racepane.fxml index fb795e5c..d699ff46 100644 --- a/src/main/resources/scenes/racepane.fxml +++ b/src/main/resources/scenes/racepane.fxml @@ -7,13 +7,15 @@ - + - + + - +