Created countdown clock on start screen.

#story[760]
main
Erika Savell 9 years ago
parent f5472bf595
commit dc32868227

@ -1,5 +1,7 @@
package seng302.Controllers;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
@ -11,7 +13,6 @@ import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import org.xml.sax.SAXException;
import seng302.Model.BoatInRace;
import seng302.Model.Race;
import seng302.Model.RaceClock;
import seng302.RaceXMLReader;
@ -32,6 +33,8 @@ public class StartController extends Controller {
@FXML private TableColumn<BoatInRace, String> boatNameColumn;
@FXML private TableColumn<BoatInRace, String> boatCodeColumn;
@FXML private Label timeZoneTime;
@FXML private Label timer;
@FXML private int PRERACE_TIME = 15000;
private RaceClock raceClock;
@ -56,10 +59,8 @@ public class StartController extends Controller {
startRace(1);
}
private void startRace(Integer raceScale){
start.setVisible(false);
parent.beginRace(raceScale);
private void startRace(int raceScale){
countdownTimer(raceScale);
}
@Override
@ -95,7 +96,54 @@ public class StartController extends Controller {
raceClock = new RaceClock(raceXMLReader.getMark());
timeZoneTime.textProperty().bind(raceClock.timeProperty());
}
/**
* Updates the calculated time to the timer label
*
* @param time The calculated time from calcTimer() method
*/
protected void updateTime(String time) {
Platform.runLater(() -> {
timer.setText(time);
});
}
/**
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
*/
protected void countdownTimer(int scaleFactor) {
new AnimationTimer() {
long currentTime = System.currentTimeMillis();
long startTime = currentTime + (PRERACE_TIME/scaleFactor);
long minutes;
long currentTimeInSeconds;
long remainingSeconds;
long hours;
long timeLeft;
@Override
public void handle(long arg0) {
timeLeft = startTime - currentTime;
if (timeLeft <= 0) {
updateTime("Race is starting...");
stop();
parent.beginRace(scaleFactor);
startWrapper.setVisible(false);
} else {
currentTimeInSeconds = (timeLeft*scaleFactor) / 1000;
minutes = currentTimeInSeconds / 60;
remainingSeconds = currentTimeInSeconds % 60;
hours = minutes / 60;
minutes = minutes % 60;
updateTime(String.format("Race Clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
}
currentTime = System.currentTimeMillis();
}
}.start();
}
}

@ -1,23 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="startWrapper" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.StartController">
<AnchorPane fx:id="startWrapper" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.StartController">
<children>
<GridPane fx:id="start" prefHeight="600.0" prefWidth="780.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
@ -56,6 +51,7 @@
</columns>
</TableView>
<Label fx:id="timeZoneTime" contentDisplay="CENTER" text="timeZoneTime" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
<Label fx:id="timer" text=" " GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="5" />
</children>
</GridPane>
</children>

Loading…
Cancel
Save