From 403aaa76aef95d70404b0ce6a5fcdc5ce929cebb Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Thu, 23 Mar 2017 18:04:00 +1300 Subject: [PATCH] Display team name and speed beside boat - Also slowed down the AnimationTimer - Removed the need to scale the canvas Tags: #implement #story[18] #story[19] --- .../seng302/controllers/CanvasController.java | 64 ++++++++++++------- src/main/java/seng302/models/Boat.java | 4 ++ src/main/java/seng302/models/Race.java | 2 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index c589c635..27241d4a 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -10,6 +10,8 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; +import javafx.scene.text.Font; +import javafx.scene.text.FontSmoothingType; import javafx.scene.text.Text; import javafx.util.Duration; import seng302.models.Boat; @@ -53,6 +55,8 @@ public class CanvasController { private Animation.Status raceStatus = Animation.Status.PAUSED; + private final int SCALE = 16000; + /** * Display the list of boats in the order they finished the race */ @@ -125,7 +129,7 @@ public class CanvasController { */ public void initialize() { gc = canvas.getGraphicsContext2D(); - gc.scale(15, 15); + //gc.scale(1, 1); RaceController raceController = new RaceController(); raceController.initializeRace(); race = raceController.getRace(); @@ -133,20 +137,26 @@ public class CanvasController { // overriding the handle so that it can clean canvas and redraw boats and course marks AnimationTimer timer = new AnimationTimer() { + private long lastUpdate = 0; + @Override public void handle(long now) { - gc.clearRect(0, 0, 760, 360); - drawCourse(); - drawBoats(); + if (now - lastUpdate >= 16000000){ + gc.clearRect(0, 0, 19200, 10800); + drawCourse(); + drawBoats(); - // If race has started, draw the boats and play the timeline - if (race.getRaceTime() > 1){ - playTimelines(); + // If race has started, draw the boats and play the timeline + if (race.getRaceTime() > 1){ + playTimelines(); - } - // Race has not started, pause the timelines - else if (race.getRaceTime() < 1 || raceStatus == Animation.Status.RUNNING){ - pauseTimelines(); + } + // Race has not started, pause the timelines + else if (race.getRaceTime() < 1 || raceStatus == Animation.Status.RUNNING){ + pauseTimelines(); + } + + lastUpdate = now; } } @@ -232,7 +242,7 @@ public class CanvasController { private void drawBoats() { for (Boat boat : timelineInfos.keySet()) { TimelineInfo timelineInfo = timelineInfos.get(boat); - drawBoat(timelineInfo.getX().doubleValue(), timelineInfo.getY().doubleValue(), boat.getColor()); + drawBoat(timelineInfo.getX().doubleValue(), timelineInfo.getY().doubleValue(), boat.getColor(), boat.getTeamName(), boat.getSpeedInKnots()); } } @@ -243,14 +253,20 @@ public class CanvasController { * @param lon * @param color */ - private void drawBoat(double lat, double lon, Color color) { + private void drawBoat(double lat, double lon, Color color, String name, double speed) { // Latitude - double x = (lon - ORIGIN_LON) * 1000; - double y = (ORIGIN_LAT - lat) * 1000; + double x = (lon - ORIGIN_LON) * SCALE; + double y = (ORIGIN_LAT - lat) * SCALE; - double diameter = 0.5; + double diameter = 9; + // Set boat text + gc.setFont(new Font(14)); gc.setFill(color); + gc.setLineWidth(3); + gc.setFontSmoothingType(FontSmoothingType.GRAY); + gc.fillText(name + ", " + speed + " knots",x+15,y+15); + gc.fillOval(x, y, diameter, diameter); } @@ -273,11 +289,11 @@ public class CanvasController { * @param singleMark */ private void drawSingleMark(SingleMark singleMark, Color color) { - double x = (singleMark.getLongitude() - ORIGIN_LON) * 1000; - double y = (ORIGIN_LAT - singleMark.getLatitude()) * 1000; + double x = (singleMark.getLongitude() - ORIGIN_LON) * SCALE; + double y = (ORIGIN_LAT - singleMark.getLatitude()) * SCALE; gc.setFill(color); - gc.fillRect(x,y,0.5,0.5); + gc.fillRect(x,y,5.5,5.5); } /** @@ -304,13 +320,13 @@ public class CanvasController { gc.setStroke(color); // Convert lat/lon to x,y - double x1 = (gateMark.getSingleMark1().getLongitude()- ORIGIN_LON) * 1000; - double y1 = (ORIGIN_LAT - gateMark.getSingleMark1().getLatitude()) * 1000; + double x1 = (gateMark.getSingleMark1().getLongitude()- ORIGIN_LON) * SCALE; + double y1 = (ORIGIN_LAT - gateMark.getSingleMark1().getLatitude()) * SCALE; - double x2 = (gateMark.getSingleMark2().getLongitude() - ORIGIN_LON) * 1000; - double y2 = (ORIGIN_LAT - gateMark.getSingleMark2().getLatitude()) * 1000; + double x2 = (gateMark.getSingleMark2().getLongitude() - ORIGIN_LON) * SCALE; + double y2 = (ORIGIN_LAT - gateMark.getSingleMark2().getLatitude()) * SCALE; - gc.setLineWidth(0.07); + gc.setLineWidth(1); gc.strokeLine(x1, y1, x2, y2); } diff --git a/src/main/java/seng302/models/Boat.java b/src/main/java/seng302/models/Boat.java index af1bb559..8aaa4e7f 100644 --- a/src/main/java/seng302/models/Boat.java +++ b/src/main/java/seng302/models/Boat.java @@ -97,4 +97,8 @@ public class Boat { public Color getColor() { return color; } + + public double getSpeedInKnots(){ + return Math.round((this.velocity * 1.94384) * 100d) / 100d; + } } \ No newline at end of file diff --git a/src/main/java/seng302/models/Race.java b/src/main/java/seng302/models/Race.java index 2ad111e8..884be6b9 100644 --- a/src/main/java/seng302/models/Race.java +++ b/src/main/java/seng302/models/Race.java @@ -16,7 +16,7 @@ public class Race { private long startTime = 0; private double timeScale = 1; private boolean raceFinished = false; // Race is finished - private int raceTime = -10; // Current time in the race + private int raceTime = -2; // Current time in the race /** * Race class containing the boats and legs in the race