Fixed arrow Direction, and Canvas

- Canvas will not longer not fill the entire screen
- Canvas will redraw when resized larger
- Arrow Rotation no longer rotates the entire map.
#fix
main
Fan-Wu Yang 9 years ago
commit 3751ee2488

@ -1,20 +1,32 @@
package seng302.Controllers; package seng302.Controllers;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint; import javafx.scene.paint.Paint;
import javafx.util.Callback;
import seng302.GPSCoordinate;
import javafx.scene.transform.Rotate; import javafx.scene.transform.Rotate;
import seng302.GraphCoordinate; import seng302.GraphCoordinate;
import seng302.Model.ResizableRaceCanvas; import seng302.Model.ResizableRaceCanvas;
import seng302.Model.*;
import seng302.RaceMap; import seng302.RaceMap;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.ResourceBundle; import java.util.ResourceBundle;
/** /**
@ -27,10 +39,29 @@ public class RaceController extends Controller{
AnchorPane canvasBase; AnchorPane canvasBase;
@FXML @FXML
ResizableRaceCanvas raceMap; ResizableRaceCanvas raceMap;
@FXML
TableColumn<BoatInRace, String> boatPlacingColumn;
@FXML
TableColumn<BoatInRace, String> boatTeamColumn;
@FXML
TableColumn<BoatInRace, String> boatMarkColumn;
private GraphicsContext gc; private GraphicsContext gc;
private RaceMap map; private RaceMap map;
public void updateInfoTable(Race race) {
boatInfoTable.setItems(FXCollections.observableArrayList(race.getFinishingBoats()));
boatTeamColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace,String>("Name"));
boatMarkColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace, String>("CurrentLeg"));
boatPlacingColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<BoatInRace, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<BoatInRace, String> table) {
return new ReadOnlyObjectWrapper(boatInfoTable.getItems().indexOf(table.getValue()) + 1);
}
});
}
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
raceMap = new ResizableRaceCanvas(); raceMap = new ResizableRaceCanvas();
@ -38,5 +69,16 @@ public class RaceController extends Controller{
raceMap.heightProperty().bind(canvasBase.heightProperty()); raceMap.heightProperty().bind(canvasBase.heightProperty());
raceMap.draw(); raceMap.draw();
canvasBase.getChildren().add(raceMap); canvasBase.getChildren().add(raceMap);
BoatInRace boat = new BoatInRace("NZ", 10000);
BoatInRace[] boats = new BoatInRace[] {boat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(1, 1), 0);
Leg[] legs = new Leg[] {leg1};
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs, this);
(new Thread(race)).start();
} }
} }

@ -27,6 +27,10 @@ public class Boat {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
/** /**
* *
* @return returns the speed of the boat. * @return returns the speed of the boat.

@ -1,5 +1,6 @@
package seng302.Model; package seng302.Model;
import seng302.Controllers.RaceController;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
import seng302.GraphCoordinate; import seng302.GraphCoordinate;
@ -17,6 +18,10 @@ public class ConstantVelocityRace extends Race {
* @see Leg * @see Leg
*/ */
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks, RaceController controller) {
super(startingBoats, marks, controller);
}
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks) { public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks) {
super(startingBoats, marks); super(startingBoats, marks);
} }

@ -28,6 +28,14 @@ public class Leg {
this.legNumber = number; this.legNumber = number;
} }
public Leg(String name) {
this.name = name;
this.distance = 0;
this.startGPSCoordinate = new GPSCoordinate(0,0);
this.endGPSCoordinate = new GPSCoordinate(0,0);
this.legNumber = 0;
}
/** /**
* *
* @return the name of the Leg * @return the name of the Leg

@ -1,16 +1,20 @@
package seng302.Model; package seng302.Model;
import seng302.Controllers.RaceController;
import seng302.GPSCoordinate;
import java.util.*; import java.util.*;
/** /**
* Parent class for races * Parent class for races
* Created by fwy13 on 3/03/17. * Created by fwy13 on 3/03/17.
*/ */
public abstract class Race { public abstract class Race implements Runnable {
protected BoatInRace[] startingBoats; protected BoatInRace[] startingBoats;
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>(); protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
protected Leg[] legs; protected Leg[] legs;
protected RaceController controller;
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
@ -19,16 +23,22 @@ public abstract class Race {
* @param boats Takes in an array of boats that are participating in the race. * @param boats Takes in an array of boats that are participating in the race.
* @param marks Number of marks in order that the boats pass in order to complete the race. * @param marks Number of marks in order that the boats pass in order to complete the race.
*/ */
public Race(BoatInRace[] boats, Leg[] marks) { public Race(BoatInRace[] boats, Leg[] marks, RaceController controller) {
this.startingBoats = boats; this.startingBoats = boats;
this.legs = marks; this.legs = marks;
this.controller = controller;
}
public Race(BoatInRace[] boats, Leg[] marks) {
this.startingBoats = boats;
this.legs = marks;
} }
public void run() { public void run() {
long time = System.currentTimeMillis();
preRace(); preRace();
simulateRace(); simulateRace();
if(controller != null) controller.updateInfoTable(this);
} }
private void preRace() { private void preRace() {
@ -79,6 +89,7 @@ public abstract class Race {
if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) { if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) {
//boat has finished //boat has finished
boat.setTimeFinished(timeElapsed); boat.setTimeFinished(timeElapsed);
boat.setCurrentLeg(new Leg("Finish"));
finishingBoats.add(boat); finishingBoats.add(boat);
} else { } else {
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance()); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
@ -90,6 +101,11 @@ public abstract class Race {
} }
public ArrayList<BoatInRace> getFinishingBoats() {
return finishingBoats;
}
/** /**
* This function is a function that generates the Race and populates the events list. * This function is a function that generates the Race and populates the events list.
* Is automatically called by the initialiser function, so that simulateRace() does not return an empty race. * Is automatically called by the initialiser function, so that simulateRace() does not return an empty race.

@ -50,14 +50,16 @@ public class ResizableRaceCanvas extends Canvas {
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 10, 10); gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 10, 10);
} }
public void displayArrow(GraphCoordinate coordinate, Paint paint){ public void displayArrow(GraphCoordinate coordinate, int angle){
rotate(10, 30,30); gc.save();
gc.fillPolygon(new double[]{0, 6, 12, 8, 8, 4, 4}, rotate(angle, coordinate.getX(),coordinate.getY());
new double[]{15, 0, 15, 15, 40, 40, 15}, gc.fillPolygon(new double[]{coordinate.getX()-12, coordinate.getX()-6, coordinate.getX(), coordinate.getX()-4, coordinate.getX()-4, coordinate.getX()-8, coordinate.getX()-8},
new double[]{coordinate.getY()-5, coordinate.getY()-20, coordinate.getY()-5, coordinate.getY()-5, coordinate.getY()+20, coordinate.getY()+20, coordinate.getY()-5},
7); 7);
gc.restore(); gc.restore();
} }
private void rotate(double angle, double px, double py) { private void rotate(double angle, double px, double py) {
Rotate r = new Rotate(angle, px, py); Rotate r = new Rotate(angle, px, py);
gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy()); gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy());
@ -75,29 +77,31 @@ public class ResizableRaceCanvas extends Canvas {
System.out.println("Drawing"); System.out.println("Drawing");
gc.clearRect(0, 0, width, height); gc.clearRect(0, 0, width, height);
//boat //boat
GraphCoordinate boat1coord = map.convertGPS(32.296577, -64.854304); GraphCoordinate boat1coord = this.map.convertGPS(32.296577, -64.854304);
displayBoat(boat1coord, Color.AQUAMARINE); displayBoat(boat1coord, Color.AQUAMARINE);
//finish line //finish line
gc.setLineWidth(2); gc.setLineWidth(2);
GraphCoordinate finishLineCoord1 = map.convertGPS(32.317379, -64.839291); GraphCoordinate finishLineCoord1 = this.map.convertGPS(32.317379, -64.839291);
GraphCoordinate finishLineCoord2 = map.convertGPS(32.317257, -64.836260); GraphCoordinate finishLineCoord2 = this.map.convertGPS(32.317257, -64.836260);
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED); displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
//marks //marks
GraphCoordinate markCoord = map.convertGPS(32.293039, -64.843983); GraphCoordinate markCoord = this.map.convertGPS(32.293039, -64.843983);
GraphCoordinate southGate1 = map.convertGPS(32.284680, -64.850045); GraphCoordinate southGate1 = this.map.convertGPS(32.284680, -64.850045);
GraphCoordinate southGate2 = map.convertGPS(32.280164, -64.847591); GraphCoordinate southGate2 = this.map.convertGPS(32.280164, -64.847591);
GraphCoordinate northGate1 = map.convertGPS(32.309693, -64.835249); GraphCoordinate northGate1 = this.map.convertGPS(32.309693, -64.835249);
GraphCoordinate northGate2 = map.convertGPS(32.308046, -64.831785); GraphCoordinate northGate2 = this.map.convertGPS(32.308046, -64.831785);
displayBoat(boat1coord, Color.AQUAMARINE); displayBoat(boat1coord, Color.AQUAMARINE);
displayBoat(markCoord, Color.GOLD); displayBoat(markCoord, Color.GOLD);
displayLine(southGate1, southGate2, Color.DARKCYAN); displayLine(southGate1, southGate2, Color.DARKCYAN);
displayLine(northGate1, northGate2, Color.DARKVIOLET); displayLine(northGate1, northGate2, Color.DARKVIOLET);
//start line //start line
GraphCoordinate startline1 = map.convertGPS(32.296577, -64.854304); GraphCoordinate startline1 = this.map.convertGPS(32.296577, -64.854304);
GraphCoordinate startline2 = map.convertGPS(32.293771, -64.855242); GraphCoordinate startline2 = this.map.convertGPS(32.293771, -64.855242);
displayLine(startline1, startline2, Color.GREEN); displayLine(startline1, startline2, Color.GREEN);
displayArrow(new GraphCoordinate(100, 100), Color.BLUEVIOLET); //display wind direction arrow - specify origin point and angle
displayPoint(this.map.convertGPS(32.293771, -64.855242), Color.BLACK);
displayArrow(new GraphCoordinate(500, 20), 100);
} }
@Override @Override

@ -15,8 +15,9 @@
<children> <children>
<TableView fx:id="boatInfoTable" prefHeight="400.0" prefWidth="146.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.bottomAnchor="0.0"> <TableView fx:id="boatInfoTable" prefHeight="400.0" prefWidth="146.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.bottomAnchor="0.0">
<columns> <columns>
<TableColumn prefWidth="75.0" text="C1" /> <TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place" />
<TableColumn prefWidth="75.0" text="C2" /> <TableColumn fx:id="boatTeamColumn" prefWidth="50.0" text="Team" />
<TableColumn fx:id="boatMarkColumn" prefWidth="50.0" text="Mark" />
</columns> </columns>
</TableView> </TableView>
</children> </children>

Loading…
Cancel
Save