Merge branch 'boundaries'

# Conflicts:
#	src/main/java/seng302/Controllers/RaceController.java
#	src/main/java/seng302/Model/ResizableRaceCanvas.java
#	src/main/resources/scenes/racepane.fxml
main
Fan-Wu Yang 9 years ago
commit 461d894ac8

@ -32,7 +32,8 @@ public class App extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
primaryStage.minHeightProperty().setValue(600);
primaryStage.minWidthProperty().setValue(780);
//load the first container
try {
FXMLLoader loader = new FXMLLoader();

@ -12,6 +12,7 @@ import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;
@ -31,13 +32,12 @@ import java.util.ResourceBundle;
*/
public class RaceController extends Controller{
@FXML
AnchorPane canvasBase;
GridPane canvasBase;
ResizableRaceCanvas raceMap;
@FXML
GridPane startScreen;
@FXML
SplitPane ongoingRacePane;
@ -166,12 +166,16 @@ public class RaceController extends Controller{
BoatInRace[] boats = new BoatInRace[raceXMLReader.getBoats().size()];
boats = raceXMLReader.getBoats().toArray(boats);
//BoatInRace[] boats = generateAC35Competitors();
raceMap = new ResizableRaceCanvas();
double lat1 = raceXMLReader.getMapTopLeft().getLatitude();
double long1 = raceXMLReader.getMapTopLeft().getLongitude();
double lat2 = raceXMLReader.getMapBottomRight().getLatitude();
double long2 = raceXMLReader.getMapBottomRight().getLongitude();
raceMap = new ResizableRaceCanvas(lat1, long1, lat2, long2);
raceMap.setMouseTransparent(true);
raceMap.widthProperty().bind(canvasBase.widthProperty());
raceMap.heightProperty().bind(canvasBase.heightProperty());
raceMap.setBoats(boats);
raceMap.setRaceBoundaries(raceXMLReader.getBoundary());
raceMap.drawBoats();
raceMap.drawRaceMap();
raceMap.setVisible(true);
@ -184,8 +188,17 @@ public class RaceController extends Controller{
ArrayList<Leg> legs = raceXMLReader.getLegs();
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs, this, scaleFactor);
//start race
new Thread((race)).start();
showFPS.setVisible(true);
showFPS.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
if (showFPS.isSelected()){
FPS.setVisible(true);
} else {
FPS.setVisible(false);
}
}
});
//listener for annotation
showAnno.selectedProperty().addListener(new ChangeListener<Boolean>() {
@ -195,6 +208,8 @@ public class RaceController extends Controller{
raceMap.update();
}
});
new Thread((race)).start();
}

@ -1,6 +1,7 @@
package seng302.Model;
import com.sun.corba.se.impl.orbutil.graph.Graph;
import javafx.application.Platform;
import javafx.beans.property.StringProperty;
import javafx.scene.canvas.Canvas;
@ -29,7 +30,10 @@ public class ResizableRaceCanvas extends Canvas {
private GraphicsContext gc;
private RaceMap map;
private BoatInRace[] boats;
private RaceController controller;
private boolean raceAnno = true;
private ArrayList<GPSCoordinate> raceBoundaries;
double[] xpoints = {}, ypoints = {};
/**
* Sets the boats that are to be displayed in this race.
@ -41,6 +45,7 @@ public class ResizableRaceCanvas extends Canvas {
}
public ResizableRaceCanvas(RaceMap map) {
super();
this.map = map;
gc = this.getGraphicsContext2D();
// Redraw canvas when size changes.
@ -53,6 +58,12 @@ public class ResizableRaceCanvas extends Canvas {
*/
public ResizableRaceCanvas() {
this(null);
setMap(new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) getWidth(), (int) getHeight()));
}
public ResizableRaceCanvas(double lat1, double long1, double lat2, double long2){
this(null);
setMap(new RaceMap(lat1, long1, lat2, long2, (int) getWidth(), (int) getHeight()));
}
/**
@ -178,6 +189,15 @@ public class ResizableRaceCanvas extends Canvas {
this.updateBoats();
}
public void drawBoundaries(){
if (this.raceBoundaries == null){
return;
}
gc.setFill(Color.AQUA);
setRaceBoundCoordinates();
gc.fillPolygon(xpoints, ypoints, xpoints.length);
}
/**
* Draws the Race Map
*/
@ -188,10 +208,15 @@ public class ResizableRaceCanvas extends Canvas {
gc.clearRect(0, 0, width, height);
//System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height);
this.map = new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) width, (int) height);
if (map == null) {
return;//TODO this should return a exception in the future
}
this.map.setHeight((int)height);
this.map.setWidth((int)width);
//finish line
gc.setLineWidth(2);
drawBoundaries();
GraphCoordinate finishLineCoord1 = this.map.convertGPS(Constants.finishLineMarker1);
GraphCoordinate finishLineCoord2 = this.map.convertGPS(Constants.finishLineMarker2);
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
@ -216,6 +241,19 @@ public class ResizableRaceCanvas extends Canvas {
displayArrow(new GraphCoordinate((int)getWidth()-40, 40), 150);
}
/**
* Draws a boat at a certain GPSCoordinate
* @param colour Colour to colour boat.
* @param gpsCoordinates GPScoordinate that the boat is to be drawn at.
* @see GPSCoordinate
* @see Color
*/
public void drawBoat(Color colour, GPSCoordinate gpsCoordinates) {
GraphCoordinate graphCoordinate = this.map.convertGPS(gpsCoordinates);
//System.out.println("DrawingBoat" + gpsCoordinates.getLongitude());
displayPoint(graphCoordinate, colour);
}
/**
* Toggle the raceAnno value
*/
@ -251,6 +289,24 @@ public class ResizableRaceCanvas extends Canvas {
}
}
public void setRaceBoundaries(ArrayList<GPSCoordinate> boundaries) {
this.raceBoundaries = new ArrayList<>();
for (GPSCoordinate bound: boundaries){
raceBoundaries.add(bound);
}
setRaceBoundCoordinates();
}
public void setRaceBoundCoordinates(){
xpoints = new double[this.raceBoundaries.size()];
ypoints = new double[this.raceBoundaries.size()];
for (int i = 0; i < raceBoundaries.size(); i++){
GraphCoordinate coord = map.convertGPS(raceBoundaries.get(i));
xpoints[i] = coord.getX();
ypoints[i] = coord.getY();
}
}
/**
* Set the Canvas to resizable.
*

@ -35,8 +35,16 @@ public class RaceMap {
* @see GraphCoordinate
*/
public GraphCoordinate convertGPS(double lat, double lon) {
int difference = Math.abs(width - height);
int size = width;
if (width > height){
size = height;
return new GraphCoordinate((int) ((size * (lon - x1) / (x2 - x1)) + difference/2), (int) (size - (size * (lat - y1) / (y2 - y1))));
}else{
return new GraphCoordinate((int) (size * (lon - x1) / (x2 - x1)), (int) ((size - (size * (lat - y1) / (y2 - y1))) + difference/2));
}
return new GraphCoordinate((int) (width * (lon - x1) / (x2 - x1)), (int) (height - (height * (lat - y1) / (y2 - y1))));
//return new GraphCoordinate((int) (width * (lon - x1) / (x2 - x1)), (int) (height - (height * (lat - y1) / (y2 - y1))));
}
/**
@ -50,4 +58,12 @@ public class RaceMap {
public GraphCoordinate convertGPS(GPSCoordinate coordinate) {
return convertGPS(coordinate.getLatitude(), coordinate.getLongitude());
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}

@ -19,7 +19,9 @@ public class RaceXMLReader extends XMLReader{
private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system.
private ArrayList<Leg> legs = new ArrayList<>();
private GPSCoordinate mark, startPt1, startPt2, finishPt1, finishPt2, leewardPt1, leewardPt2, windwardPt1, windwardPt2;
private GPSCoordinate mapTopLeft, mapBottomRight;
private ArrayList<GPSCoordinate> boundary = new ArrayList<>();
private static double COORDINATEPADDING = 0.0005;
public RaceXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException {
@ -76,9 +78,61 @@ public class RaceXMLReader extends XMLReader{
NodeList nCourse = doc.getElementsByTagName("course");
NodeList nBounds = ((Element)nCourse.item(0)).getElementsByTagName("boundaries");
nBounds = ((Element) nBounds.item(0)).getElementsByTagName("coordinate");
int maxLatitudeIndex = 0;
double maxLatitude = -Double.MIN_VALUE;
int maxLongitudeIndex = 0;
double maxLongitude = -180;
int minLatitudeIndex = 0;
double minLatitude = Double.MAX_VALUE;
int minLongitudeIndex = 0;
double minLongitude = Double.MAX_VALUE;
for (int i = 0; i < nBounds.getLength(); i++){
boundary.add(getCoordinates(nBounds, i));
}
boundary.add(getCoordinates((Element) nBounds.item(i)));
if (boundary.get(i).getLatitude() > maxLatitude){
maxLatitudeIndex = i;
maxLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLatitude() < minLatitude){
minLatitudeIndex = i;
minLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLongitude() > maxLongitude){
maxLongitudeIndex = i;
maxLongitude = boundary.get(i).getLongitude();
}
if (boundary.get(i).getLongitude() < minLongitude){
minLongitudeIndex = i;
minLongitude = boundary.get(i).getLongitude();
}
}
/*System.out.println(nBounds.getLength());
System.out.println(maxLatitude);
System.out.println(minLatitude);
System.out.println(maxLongitude);
System.out.println(minLongitude);*/
double difference = 0;//this will hold the largest difference so we can make the map square.
double latitudeDiff = Math.abs(Math.abs(boundary.get(maxLatitudeIndex).getLatitude()) - Math.abs(boundary.get(minLatitudeIndex).getLatitude()));
double longitudeDiff = Math.abs(Math.abs(boundary.get(maxLongitudeIndex).getLongitude()) - Math.abs(boundary.get(minLongitudeIndex).getLongitude()));
if (latitudeDiff >= longitudeDiff){
difference = latitudeDiff - longitudeDiff;
maxLongitude += difference/2;
minLongitude -= difference/2;
}else{
difference = longitudeDiff - latitudeDiff;
maxLatitude += difference/2;
minLatitude -= difference/2;
}
maxLatitude += COORDINATEPADDING;
minLatitude -= COORDINATEPADDING;
maxLongitude += COORDINATEPADDING;
minLongitude -= COORDINATEPADDING;
//now create map boundaries
//top left canvas point is min logitude, max latitude
//bottom right of canvas point is min longitude, max latitude.
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
NodeList nMarks = ((Element)nCourse.item(0)).getElementsByTagName("marker");
startPt1 = getCoordinates(nMarks, 0);
@ -165,4 +219,12 @@ public class RaceXMLReader extends XMLReader{
public ArrayList<GPSCoordinate> getBoundary() {
return boundary;
}
public GPSCoordinate getMapTopLeft() {
return mapTopLeft;
}
public GPSCoordinate getMapBottomRight() {
return mapBottomRight;
}
}

@ -119,12 +119,48 @@
<course>
<boundaries>
<coordinate>
<latitude>32.278</latitude>
<longitude>-64.863</longitude>
<latitude>32.313922</latitude>
<longitude>-64.837168</longitude>
</coordinate>
<coordinate>
<latitude>32.30989</latitude>
<longitude>-64.821</longitude>
<latitude>32.317403</latitude>
<longitude>-64.838627</longitude>
</coordinate>
<coordinate>
<latitude>32.317911</latitude>
<longitude>-64.836996</longitude>
</coordinate>
<coordinate>
<latitude>32.317548</latitude>
<longitude>-64.835022</longitude>
</coordinate>
<coordinate>
<latitude>32.304273</latitude>
<longitude>-64.822834</longitude>
</coordinate>
<coordinate>
<latitude>32.279097</latitude>
<longitude>-64.841545</longitude>
</coordinate>
<coordinate>
<latitude>32.279604</latitude>
<longitude>-64.849871</longitude>
</coordinate>
<coordinate>
<latitude>32.289545</latitude>
<longitude>-64.854162</longitude>
</coordinate>
<coordinate>
<latitude>32.290198</latitude>
<longitude>-64.858711</longitude>
</coordinate>
<coordinate>
<latitude>32.297164</latitude>
<longitude>-64.856394</longitude>
</coordinate>
<coordinate>
<latitude>32.296148</latitude>
<longitude>-64.849184</longitude>
</coordinate>
</boundaries>
<marker>

@ -6,8 +6,8 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="seng302.Controllers.RaceController">
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController">
<children>
<GridPane fx:id="startScreen" prefHeight="600.0" prefWidth="780.0">
<columnConstraints>

Loading…
Cancel
Save