Started to implement the group over the canvas in the code. Removed basic boat redrawing and timeline and replaced with boats being placed into a group and given coordinates.

This commit is contained in:
Kusal Ekanayake
2017-04-11 17:46:02 +12:00
parent 9817fc9093
commit 15ded667fe
4 changed files with 232 additions and 57 deletions
@@ -1,7 +1,9 @@
package seng302.controllers;
import javafx.animation.*;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.AnchorPane;
@@ -14,6 +16,7 @@ import seng302.models.mark.Mark;
import seng302.models.mark.MarkType;
import seng302.models.mark.SingleMark;
import java.sql.Time;
import java.util.*;
/**
@@ -27,6 +30,7 @@ public class CanvasController {
private RaceViewController raceViewController;
private ResizableCanvas canvas;
private Group group;
private GraphicsContext gc;
private final double ORIGIN_LAT = 32.321504;
@@ -38,30 +42,51 @@ public class CanvasController {
}
public void initialize() {
raceViewController = new RaceViewController();
canvas = new ResizableCanvas();
group = new Group();
canvasPane.getChildren().add(canvas);
canvasPane.getChildren().add(group);
// Bind canvas size to stack pane size.
canvas.widthProperty().bind(canvasPane.widthProperty());
canvas.heightProperty().bind(canvasPane.heightProperty());
canvas.widthProperty().bind(new SimpleDoubleProperty(1000));
canvas.heightProperty().bind(new SimpleDoubleProperty(1000));
group.minWidth(1000);
group.minHeight(1000);
// canvas.widthProperty().bind(canvasPane.widthProperty());
// canvas.heightProperty().bind(canvasPane.heightProperty());
// group.minWidth(canvas.getWidth());
// group.minHeight(canvas.getHeight());
}
public void setUpBoats(){
gc = canvas.getGraphicsContext2D();
gc.save();
gc.setFill(Color.SKYBLUE);
gc.fillRect(0,0, 1000,1000);
gc.restore();
drawBoats();
drawCourse();
drawFps(12);
// overriding the handle so that it can clean canvas and redraw boats and course marks
AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
private long lastFpsUpdate = 0;
private int lastFpsCount = 0;
private int fpsCount = 0;
boolean done = true;
@Override
public void handle(long now) {
if (true){ //if statement for limiting refresh rate if needed
gc.clearRect(0, 0, canvas.getWidth(),canvas.getHeight());
gc.setFill(Color.SKYBLUE);
gc.fillRect(0,0,canvas.getWidth(),canvas.getHeight());
drawCourse();
drawBoats();
drawFps(lastFpsCount);
// gc.clearRect(0, 0, canvas.getWidth(),canvas.getHeight());
// gc.setFill(Color.SKYBLUE);
// gc.fillRect(0,0,canvas.getWidth(),canvas.getHeight());
// If race has started, draw the boats and play the timeline
if (raceViewController.getRace().getRaceTime() > 1){
@@ -129,14 +154,26 @@ public class CanvasController {
* Draws all the boats.
*/
private void drawBoats() {
Map<Boat, TimelineInfo> timelineInfos = raceViewController.getTimelineInfos();
for (Boat boat : timelineInfos.keySet()) {
TimelineInfo timelineInfo = timelineInfos.get(boat);
// Map<Boat, TimelineInfo> timelineInfos = raceViewController.getTimelineInfos();
ArrayList<Boat> boats = raceViewController.getStartingBoats();
Double startingY = (ORIGIN_LAT - raceViewController.getRace().getCourse().get(0).getLatitude()) * SCALE;
Double startingX = (ORIGIN_LON - raceViewController.getRace().getCourse().get(0).getLongitude()) * SCALE;
boat.setLocation(timelineInfo.getY().doubleValue(), timelineInfo.getX().doubleValue());
drawBoat(boat.getLongitude(), boat.getLatitude(), boat.getColor(), boat.getShortName(), boat.getSpeedInKnots(), boat.getHeading());
for (Boat boat : boats) {
boat.moveBoatTo(startingX, startingY);
group.getChildren().add(boat.getWake());
group.getChildren().add(boat.getBoatObject());
group.getChildren().add(boat.getTeamNameObject());
group.getChildren().add(boat.getVelocityObject());
// drawBoat(boat.getLongitude(), boat.getLatitude(), boat.getColor(), boat.getShortName(), boat.getSpeedInKnots(), boat.getHeading());
}
// for (Boat boat : timelineInfos.keySet()) {
// TimelineInfo timelineInfo = timelineInfos.get(boat);
//
// boat.setLocation(timelineInfo.getY().doubleValue(), timelineInfo.getX().doubleValue());
//
// drawBoat(boat.getLongitude(), boat.getLatitude(), boat.getColor(), boat.getShortName(), boat.getSpeedInKnots(), boat.getHeading());
// }
}
/**
@@ -42,6 +42,7 @@ public class RaceViewController {
@FXML
private CanvasController includedCanvasController;
private ArrayList<Boat> startingBoats = new ArrayList<>();
private boolean displayAnnotations;
private boolean displayFps;
private Timeline timerTimeline;
@@ -50,24 +51,29 @@ public class RaceViewController {
private Race race;
public void initialize() {
includedCanvasController.setup(this);
RaceController raceController = new RaceController();
raceController.initializeRace();
race = raceController.getRace();
for (Boat boat : race.getBoats()) {
startingBoats.add(boat);
}
// try{
// initializeTimelines();
// }
// catch (Exception e){
// e.printStackTrace();
// }
includedCanvasController.setup(this);
includedCanvasController.setUpBoats();
initializeTimer();
initializeSettings();
try{
initializeTimelines();
}
catch (Exception e){
e.printStackTrace();
}
//set wind direction!!!!!!! can't find another place to put my code --haoming
double windDirection = new ConfigParser("/config/config.xml").getWindDirection();
windDirectionText.setText(String.format("%.1f°", windDirection));
windArrowText.setRotate(windDirection);
}
private void initializeSettings(){
@@ -114,39 +120,39 @@ public class RaceViewController {
*/
private void initializeTimelines() {
HashMap<Boat, List> boat_events = race.getEvents();
for (Boat boat : boat_events.keySet()) {
// x, y are the real time coordinates
DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
List<KeyFrame> keyFrames = new ArrayList<>();
List<Event> events = boat_events.get(boat);
// iterates all events and convert each event to keyFrame, then add them into a list
for (Event event : events) {
if (event.getIsFinishingEvent()) {
keyFrames.add(
new KeyFrame(Duration.seconds(event.getTime()),
onFinished -> {race.setBoatFinished(boat); handleEvent(event);},
new KeyValue(x, event.getThisMark().getLatitude()),
new KeyValue(y, event.getThisMark().getLongitude())
)
);
} else {
keyFrames.add(
new KeyFrame(Duration.seconds(event.getTime()),
onFinished ->{
handleEvent(event);
boat.setHeading(event.getBoatHeading());
},
new KeyValue(x, event.getThisMark().getLatitude()),
new KeyValue(y, event.getThisMark().getLongitude())
)
);
}
}
timelineInfos.put(boat, new TimelineInfo(new Timeline(keyFrames.toArray(new KeyFrame[keyFrames.size()])), x, y));
startingBoats.add(boat);
// // x, y are the real time coordinates
// DoubleProperty x = new SimpleDoubleProperty();
// DoubleProperty y = new SimpleDoubleProperty();
//
// List<KeyFrame> keyFrames = new ArrayList<>();
// List<Event> events = boat_events.get(boat);
//
// // iterates all events and convert each event to keyFrame, then add them into a list
// for (Event event : events) {
// if (event.getIsFinishingEvent()) {
// keyFrames.add(
// new KeyFrame(Duration.seconds(event.getTime()),
// onFinished -> {race.setBoatFinished(boat); handleEvent(event);},
// new KeyValue(x, event.getThisMark().getLatitude()),
// new KeyValue(y, event.getThisMark().getLongitude())
// )
// );
// } else {
// keyFrames.add(
// new KeyFrame(Duration.seconds(event.getTime()),
// onFinished ->{
// handleEvent(event);
// boat.setHeading(event.getBoatHeading());
// },
// new KeyValue(x, event.getThisMark().getLatitude()),
// new KeyValue(y, event.getThisMark().getLongitude())
// )
// );
// }
// }
// timelineInfos.put(boat, new TimelineInfo(new Timeline(keyFrames.toArray(new KeyFrame[keyFrames.size()])), x, y));
}
setRaceDuration();
}
@@ -277,4 +283,8 @@ public class RaceViewController {
public Map<Boat, TimelineInfo> getTimelineInfos() {
return timelineInfos;
}
public ArrayList<Boat> getStartingBoats(){
return startingBoats;
}
}