Refactoring

#story[24]
main
Erika Savell 9 years ago
parent e2f9a710aa
commit ec9574fd9d

@ -18,6 +18,7 @@ public class App extends Application {
/**
* Entry point for running the programme
*
* @param args
*/
public static void main(String[] args) {
@ -26,6 +27,7 @@ public class App extends Application {
/**
* Loads and sets up the GUI elements
*
* @param primaryStage
* @throws Exception
*/
@ -55,6 +57,7 @@ public class App extends Application {
/**
* Loads panes for use in the GUI
*
* @param fxmlName
* @throws Exception
*/

@ -5,27 +5,16 @@ import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
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;
import org.xml.sax.SAXException;
import org.geotools.referencing.GeodeticCalculator;
import seng302.Constants;
import seng302.GPSCoordinate;
import seng302.Model.*;
import seng302.RaceXMLReader;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.awt.geom.Point2D;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
@ -33,7 +22,7 @@ import java.util.ResourceBundle;
/**
* Created by fwy13 on 15/03/2017.
*/
public class RaceController extends Controller{
public class RaceController extends Controller {
@FXML
GridPane canvasBase;
@ -43,16 +32,12 @@ public class RaceController extends Controller{
GridPane startScreen;
@FXML
SplitPane ongoingRacePane;
@FXML
CheckBox showFPS;
@FXML
CheckBox showAnno;
CheckBox showAnnotations;
@FXML
Label timer;
@FXML
Label FPS;
@ -123,12 +108,10 @@ public class RaceController extends Controller{
@Override
public void initialize(URL location, ResourceBundle resources) {
//listener for fps
/*Tooltip tp = new Tooltip("");
tp.install(timer, tp);*/
showFPS.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
if (showFPS.isSelected()){
if (showFPS.isSelected()) {
FPS.setVisible(true);
} else {
FPS.setVisible(false);
@ -154,9 +137,9 @@ public class RaceController extends Controller{
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
BoatInRace[] boats = new BoatInRace[raceXMLReader.getBoats().size()];
boats = raceXMLReader.getBoats().toArray(boats);
//BoatInRace[] boats = generateAC35Competitors();
double lat1 = raceXMLReader.getMapTopLeft().getLatitude();
double long1 = raceXMLReader.getMapTopLeft().getLongitude();
double lat2 = raceXMLReader.getMapBottomRight().getLatitude();
@ -174,14 +157,12 @@ public class RaceController extends Controller{
i++;
}
raceMap = new ResizableRaceCanvas(lat1, long1, lat2, long2);
raceMap.setMouseTransparent(true);
raceMap.widthProperty().bind(canvasBase.widthProperty());
raceMap.heightProperty().bind(canvasBase.heightProperty());
raceMap.setBoats(startingBoats);
raceMap.setRaceBoundaries(raceXMLReader.getBoundary());
//raceMap.drawBoats();
raceMap.drawRaceMap();
raceMap.setVisible(true);
@ -190,44 +171,60 @@ public class RaceController extends Controller{
ongoingRacePane.setVisible(true);
initializeFPS();
initializeAnnotations();
new Thread((race)).start();
}
/**
* Set the value for the race clock label
*
* @param time time that the label will be updated to
*/
public void setTimer(String time) {
timer.setText(time);
}
/**
* Set the value for the fps label
*
* @param fps fps that the label will be updated to
*/
public void setFrames(String fps) {
FPS.setText((fps));
}
/**
* Set up FPS display at bottom of screen
*/
private void initializeFPS() {
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()){
if (showFPS.isSelected()) {
FPS.setVisible(true);
} else {
FPS.setVisible(false);
}
}
});
}
/**
* Set up boat annotations
*/
private void initializeAnnotations() {
//listener for annotation
showAnno.selectedProperty().addListener(new ChangeListener<Boolean>() {
showAnnotations.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
raceMap.toggleAnno();
raceMap.toggleAnnotations();
raceMap.update();
}
});
new Thread((race)).start();
}
/**
* Set the value for the race clock label
* @param time time that the label will be updated to
*/
public void setTimer(String time) {
timer.setText(time);
}
/**
* Set the value for the fps label
* @param fps fps that the label will be updated to
*/
public void setFrames(String fps) { FPS.setText((fps)); }
}

@ -35,6 +35,7 @@ public class Boat {
/**
* Sets the boat name
*
* @param name
*/
public void setName(String name) {
@ -50,6 +51,7 @@ public class Boat {
/**
* Sets the speed of the boat in knots.
*
* @param velocity speed in knots
*/
public void setVelocity(double velocity) {
@ -59,6 +61,7 @@ public class Boat {
/**
* Print method prints the name of the boat
*
* @return Name of the boat.
*/
public String toString() {

@ -83,6 +83,7 @@ public class BoatInRace extends Boat {
* Returns the position of the end of the boat's wake, which is 180 degrees
* from the boat's heading, and whose length is proportional to the boat's
* speed.
*
* @return GPSCoordinate of wake endpoint.
*/
public GPSCoordinate getWake() {
@ -107,6 +108,7 @@ public class BoatInRace extends Boat {
/**
* Sets the boat's scaled velocity
*
* @param velocity
*/
public void setScaledVelocity(double velocity) {
@ -219,6 +221,7 @@ public class BoatInRace extends Boat {
/**
* Sets whether boat is finished or not
*
* @param bool
*/
public void setFinished(boolean bool) {

@ -28,6 +28,7 @@ public class ConstantVelocityRace extends Race {
/**
* Calculates the distance a boat has travelled and updates its current position according to this value.
*
* @param boat
* @param millisecondsElapsed
*/
@ -44,12 +45,13 @@ public class ConstantVelocityRace extends Race {
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
//Calculate boat's new position by adding the distance travelled onto the start point of the leg
boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartMarker().getAverageGPSCoordinate(),
totalDistanceTravelled, boat.calculateAzimuth()));
totalDistanceTravelled, boat.calculateAzimuth()));
}
}
/**
* Calculates the boats next GPS position based on its distance travelled and heading
*
* @param oldCoordinates GPS coordinates of the boat's starting position
* @param distanceTravelled distance in nautical miles
* @param azimuth boat's current direction. Value between -180 and 180

@ -1,6 +1,5 @@
package seng302.Model;
import com.oracle.xmlns.internal.webservices.jaxws_databinding.SoapBindingUse;
import org.geotools.referencing.GeodeticCalculator;
import seng302.Constants;
import seng302.GPSCoordinate;
@ -102,5 +101,4 @@ public class Leg {
}
}

@ -2,7 +2,6 @@ package seng302.Model;
import org.geotools.referencing.GeodeticCalculator;
import seng302.GPSCoordinate;
import sun.java2d.loops.GeneralRenderer;
import java.awt.geom.Point2D;

@ -3,9 +3,7 @@ package seng302.Model;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import org.geotools.referencing.GeodeticCalculator;
import seng302.Controllers.RaceController;
@ -38,8 +36,9 @@ public abstract class Race implements Runnable {
/**
* Initailiser for Race
*
* @param boats Takes in an array of boats that are participating in the race.
* @param legs Number of marks in order that the boats pass in order to complete the race.
* @param legs Number of marks in order that the boats pass in order to complete the race.
*/
public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller, int scaleFactor) {
@ -48,7 +47,7 @@ public abstract class Race implements Runnable {
this.legs.add(new Leg("Finish", this.legs.size()));
this.controller = controller;
this.scaleFactor = scaleFactor;
if (startingBoats != null && startingBoats.size() > 0){
if (startingBoats != null && startingBoats.size() > 0) {
initialiseBoats();
}
}
@ -83,7 +82,7 @@ public abstract class Race implements Runnable {
public void run() {
setControllerListeners();
initialiseBoats();
if(timerEnabled) countdownTimer();
if (timerEnabled) countdownTimer();
simulateRace();
}
@ -134,6 +133,7 @@ public abstract class Race implements Runnable {
/**
* Takes total time elapsed and format to hour:minute:second
*
* @return Formatted time as string
*/
protected String calcTimer() {
@ -142,7 +142,7 @@ public abstract class Race implements Runnable {
long remainingSeconds;
long hours;
currentTimeInSeconds = (totalTimeElapsed * scaleFactor)/ 1000;
currentTimeInSeconds = (totalTimeElapsed * scaleFactor) / 1000;
minutes = currentTimeInSeconds / 60;
remainingSeconds = currentTimeInSeconds % 60;
hours = minutes / 60;
@ -152,18 +152,24 @@ public abstract class Race implements Runnable {
/**
* Updates the calculated time to the timer label
*
* @param time The calculated time from calcTimer() method
*/
protected void updateTime(String time){
Platform.runLater(() -> {controller.setTimer(time);});
protected void updateTime(String time) {
Platform.runLater(() -> {
controller.setTimer(time);
});
}
/**
* Update the calculated fps to the fps label
*
* @param fps The new calculated fps value
*/
private void updateFPS(int fps) {
Platform.runLater(() -> {controller.setFrames("FPS: " + fps);});
Platform.runLater(() -> {
controller.setFrames("FPS: " + fps);
});
}
private boolean doNotFinish() {
@ -179,7 +185,7 @@ public abstract class Race implements Runnable {
System.setProperty("javafx.animation.fullspeed", "true");
for (BoatInRace boat: startingBoats){
for (BoatInRace boat : startingBoats) {
boat.setStarted(true);
}
@ -192,16 +198,12 @@ public abstract class Race implements Runnable {
@Override
public void handle(long arg0) {
/*long timeLoopStarted;
long timeLoopEnded;
int fps = 0;*/
if (boatsFinished < startingBoats.size()) {
//timeLoopStarted = System.currentTimeMillis();
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
for (BoatInRace boat : startingBoats) {
if (boat != null && !boat.isFinished()) {
updatePosition(boat, Math.round(1000/lastFPS) > 20 ? 15 : Math.round(1000/lastFPS));
updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
checkPosition(boat, totalTimeElapsed);
}
}
@ -216,7 +218,7 @@ public abstract class Race implements Runnable {
stop(); //exit animation timer
}
fps++;
if ((System.currentTimeMillis()-timeCurrent) > 1000){
if ((System.currentTimeMillis() - timeCurrent) > 1000) {
updateFPS(fps);
lastFPS = fps;
fps = 0;
@ -230,22 +232,23 @@ public abstract class Race implements Runnable {
/**
* Checks the position of the boat, this updates the boats current position.
* @param boat Boat that the postion is to be updated for.
*
* @param boat Boat that the postion is to be updated for.
* @param timeElapsed Time that has elapse since the start of the the race.
* @see BoatInRace
*/
protected void checkPosition(BoatInRace boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
//boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished
boatsFinished++;
boat.setFinished(true);
boat.setTimeFinished(timeElapsed);
} else if(doNotFinish()) {
} else if (doNotFinish()) {
boatsFinished++;
boat.setFinished(true);
boat.setCurrentLeg(new Leg("DNF",-1));
boat.setCurrentLeg(new Leg("DNF", -1));
boat.setVelocity(0);
boat.setScaledVelocity(0);
} else {
@ -267,11 +270,12 @@ public abstract class Race implements Runnable {
* Update call for the controller.
*/
protected void setControllerListeners() {
if(controller != null) controller.setInfoTable(this);
if (controller != null) controller.setInfoTable(this);
}
/**
* Returns the boats that have started the race.
*
* @return ObservableList of BoatInRace class that participated in the race.
* @see ObservableList
* @see BoatInRace
@ -282,6 +286,7 @@ public abstract class Race implements Runnable {
/**
* Updates the boat's gps coordinates depending on time elapsed
*
* @param boat
* @param millisecondsElapsed
*/
@ -289,7 +294,8 @@ public abstract class Race implements Runnable {
/**
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line
* @return
*
* @return list of starting positions
*/
public ArrayList<Marker> getSpreadStartingPositions() {

@ -1,9 +1,6 @@
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;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
@ -15,11 +12,7 @@ import seng302.GPSCoordinate;
import seng302.GraphCoordinate;
import seng302.RaceMap;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* This creates a JavaFX Canvas that is fills it's parent.
@ -61,7 +54,7 @@ public class ResizableRaceCanvas extends Canvas {
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){
public ResizableRaceCanvas(double lat1, double long1, double lat2, double long2) {
this(null);
setMap(new RaceMap(lat1, long1, lat2, long2, (int) getWidth(), (int) getHeight()));
}
@ -77,16 +70,17 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Displays the mark of a race as a circle.
*
* @param graphCoordinate Latitude and Logintude in GraphCoordinate that it is to be displayed as.
* @param paint Colour the mark is to be coloured.
* @param paint Colour the mark is to be coloured.
* @see GraphCoordinate
* @see Color
* @see Paint
*/
public void displayMark(GraphCoordinate graphCoordinate, Paint paint){
public void displayMark(GraphCoordinate graphCoordinate, Paint paint) {
double d = 25;
gc.setFill(paint);
gc.fillOval(graphCoordinate.getX() - (d/2), graphCoordinate.getY() - (d/2), d, d);
gc.fillOval(graphCoordinate.getX() - (d / 2), graphCoordinate.getY() - (d / 2), d, d);
}
public void displayBoat(BoatInRace boat, double angle) {
@ -105,9 +99,10 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Displays a line on the map with rectangles on the starting and ending point of the line.
*
* @param graphCoordinateA Starting Point of the line in GraphCoordinate.
* @param graphCoordinateB End Point of the line in GraphCoordinate.
* @param paint Colour the line is to coloured.
* @param paint Colour the line is to coloured.
* @see GraphCoordinate
* @see Color
* @see Paint
@ -122,8 +117,9 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Display a point on the Canvas
*
* @param graphCoordinate Coordinate that the point is to be displayed at.
* @param paint Colour that the boat is to be coloured.
* @param paint Colour that the boat is to be coloured.
* @see GraphCoordinate
* @see Paint
* @see Color
@ -136,8 +132,9 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Displays an arrow on the Canvas
*
* @param coordinate Coordinate that the arrow is to be displayed at.
* @param angle Angle that the arrow is to be facing in degrees 0 degrees = North (Up).
* @param angle Angle that the arrow is to be facing in degrees 0 degrees = North (Up).
* @see GraphCoordinate
*/
private void displayArrow(GraphCoordinate coordinate, int angle) {
@ -152,9 +149,10 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Rotates things on the canvas Note: this must be called in between gc.save() and gc.restore() else they will rotate everything
*
* @param angle Bearing angle to rotate at in degrees
* @param px Pivot point x of rotation.
* @param py Pivot point y of rotation.
* @param px Pivot point x of rotation.
* @param py Pivot point y of rotation.
*/
private void rotate(double angle, double px, double py) {
Rotate r = new Rotate(angle, px, py);
@ -163,18 +161,19 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Display given name and speed of boat at a graph coordinate
* @param name name of the boat
* @param speed speed of the boat
*
* @param name name of the boat
* @param speed speed of the boat
* @param coordinate coordinate the text appears
*/
private void displayText(String name, double speed, GraphCoordinate coordinate){
String text = String.format("%s, %2$.2fkn", name, speed);
long xCoord = coordinate.getX()+20;
private void displayText(String name, double speed, GraphCoordinate coordinate) {
String text = String.format("%s, %2$.2fkn", name, speed);
long xCoord = coordinate.getX() + 20;
long yCoord = coordinate.getY();
if (xCoord+(text.length()*7) >= getWidth()){
xCoord -= text.length()*7;
if (xCoord + (text.length() * 7) >= getWidth()) {
xCoord -= text.length() * 7;
}
if (yCoord-(text.length()*2) <= 0){
if (yCoord - (text.length() * 2) <= 0) {
yCoord += 30;
}
gc.fillText(text, xCoord, yCoord);
@ -188,8 +187,8 @@ public class ResizableRaceCanvas extends Canvas {
this.updateBoats();
}
public void drawBoundaries(){
if (this.raceBoundaries == null){
public void drawBoundaries() {
if (this.raceBoundaries == null) {
return;
}
gc.setFill(Color.AQUA);
@ -210,8 +209,8 @@ public class ResizableRaceCanvas extends Canvas {
if (map == null) {
return;//TODO this should return a exception in the future
}
this.map.setHeight((int)height);
this.map.setWidth((int)width);
this.map.setHeight((int) height);
this.map.setWidth((int) width);
//finish line
gc.setLineWidth(2);
@ -237,12 +236,13 @@ public class ResizableRaceCanvas extends Canvas {
updateBoats();
//display wind direction arrow - specify origin point and angle - angle now set to random angle
displayArrow(new GraphCoordinate((int)getWidth()-40, 40), 150);
displayArrow(new GraphCoordinate((int) getWidth() - 40, 40), 150);
}
/**
* Draws a boat at a certain GPSCoordinate
* @param colour Colour to colour boat.
*
* @param colour Colour to colour boat.
* @param gpsCoordinates GPScoordinate that the boat is to be drawn at.
* @see GPSCoordinate
* @see Color
@ -255,8 +255,8 @@ public class ResizableRaceCanvas extends Canvas {
/**
* Toggle the raceAnno value
*/
public void toggleAnno(){
if (raceAnno){
public void toggleAnnotations() {
if (raceAnno) {
raceAnno = false;
} else {
raceAnno = true;
@ -276,29 +276,30 @@ public class ResizableRaceCanvas extends Canvas {
GraphCoordinate wakeFrom = this.map.convertGPS(boat.getCurrentPosition());
GraphCoordinate wakeTo = this.map.convertGPS(boat.getWake());
displayLine(wakeFrom, wakeTo, boat.getColour());
} else if (!isStart){
} else if (!isStart) {
displayBoat(boat, boat.calculateHeading());
} else {
displayBoat(boat, 0);
}
if (raceAnno) displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
if (raceAnno)
displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
}
}
}
public void setRaceBoundaries(ArrayList<GPSCoordinate> boundaries) {
this.raceBoundaries = new ArrayList<>();
for (GPSCoordinate bound: boundaries){
for (GPSCoordinate bound : boundaries) {
raceBoundaries.add(bound);
}
setRaceBoundCoordinates();
}
public void setRaceBoundCoordinates(){
public void setRaceBoundCoordinates() {
xpoints = new double[this.raceBoundaries.size()];
ypoints = new double[this.raceBoundaries.size()];
for (int i = 0; i < raceBoundaries.size(); i++){
for (int i = 0; i < raceBoundaries.size(); i++) {
GraphCoordinate coord = map.convertGPS(raceBoundaries.get(i));
xpoints[i] = coord.getX();
ypoints[i] = coord.getY();
@ -337,17 +338,4 @@ public class ResizableRaceCanvas extends Canvas {
return getHeight();
}
/**
* Draws boats during race setup, when leg heading is not set.
*/
public void drawBoats() {
if (boats != null) {
for (BoatInRace boat : boats) {
if (boat != null) {
displayBoat(boat, 0);
if (raceAnno) displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
}
}
}
}
}

@ -37,11 +37,11 @@ public class RaceMap {
public GraphCoordinate convertGPS(double lat, double lon) {
int difference = Math.abs(width - height);
int size = width;
if (width > height){
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) ((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))));

@ -1,9 +1,9 @@
package seng302;
import javafx.scene.paint.Color;
import org.w3c.dom.*;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.Model.Boat;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Model.Marker;
@ -15,7 +15,7 @@ import java.util.ArrayList;
/**
* Created by fwy13 on 26/03/2017.
*/
public class RaceXMLReader extends XMLReader{
public class RaceXMLReader extends XMLReader {
private ArrayList<BoatInRace> boats = new ArrayList<>();
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<>();
@ -36,34 +36,34 @@ public class RaceXMLReader extends XMLReader{
}
}
private void read(){
private void read() {
readCourse();
readLegs();
readBoats();
}
public void readBoats(){
public void readBoats() {
//get all boats
NodeList nBoats = doc.getElementsByTagName("boat");
for (int i = 0; i < nBoats.getLength(); i++){
for (int i = 0; i < nBoats.getLength(); i++) {
String name = getTextValueOfNode((Element) nBoats.item(i), "name");
String abbrev = getTextValueOfNode((Element) nBoats.item(i), "abbr");
double velo = Double.parseDouble(getTextValueOfNode((Element) nBoats.item(i), "speed"));
BoatInRace boat = new BoatInRace(name, velo, colors[i], abbrev);
boat.setCurrentPosition(startPt1);
if (legs.size() > 0){
if (legs.size() > 0) {
boat.setCurrentLeg(legs.get(0));
}
boats.add(boat);
}
}
public void readLegs(){
public void readLegs() {
//get all legs
NodeList nLegs = doc.getElementsByTagName("leg");
for (int i = 0; i < nLegs.getLength(); i++){
for (int i = 0; i < nLegs.getLength(); i++) {
String label = getTextValueOfNode((Element) nLegs.item(i), "name");
NodeList start = ((Element) nLegs.item(i)).getElementsByTagName("start");
Marker startMarker = getMarker(start);
@ -73,10 +73,10 @@ public class RaceXMLReader extends XMLReader{
}
}
public void readCourse(){
public void readCourse() {
NodeList nCourse = doc.getElementsByTagName("course");
NodeList nBounds = ((Element)nCourse.item(0)).getElementsByTagName("boundaries");
NodeList nBounds = ((Element) nCourse.item(0)).getElementsByTagName("boundaries");
nBounds = ((Element) nBounds.item(0)).getElementsByTagName("coordinate");
int maxLatitudeIndex = 0;
double maxLatitude = -Double.MIN_VALUE;
@ -86,21 +86,21 @@ public class RaceXMLReader extends XMLReader{
double minLatitude = Double.MAX_VALUE;
int minLongitudeIndex = 0;
double minLongitude = Double.MAX_VALUE;
for (int i = 0; i < nBounds.getLength(); i++){
for (int i = 0; i < nBounds.getLength(); i++) {
boundary.add(getCoordinates((Element) nBounds.item(i)));
if (boundary.get(i).getLatitude() > maxLatitude){
if (boundary.get(i).getLatitude() > maxLatitude) {
maxLatitudeIndex = i;
maxLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLatitude() < minLatitude){
if (boundary.get(i).getLatitude() < minLatitude) {
minLatitudeIndex = i;
minLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLongitude() > maxLongitude){
if (boundary.get(i).getLongitude() > maxLongitude) {
maxLongitudeIndex = i;
maxLongitude = boundary.get(i).getLongitude();
}
if (boundary.get(i).getLongitude() < minLongitude){
if (boundary.get(i).getLongitude() < minLongitude) {
minLongitudeIndex = i;
minLongitude = boundary.get(i).getLongitude();
}
@ -109,14 +109,14 @@ public class RaceXMLReader extends XMLReader{
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){
if (latitudeDiff >= longitudeDiff) {
difference = latitudeDiff - longitudeDiff;
maxLongitude += difference/2;
minLongitude -= difference/2;
}else{
maxLongitude += difference / 2;
minLongitude -= difference / 2;
} else {
difference = longitudeDiff - latitudeDiff;
maxLatitude += difference/2;
minLatitude -= difference/2;
maxLatitude += difference / 2;
minLatitude -= difference / 2;
}
maxLatitude += COORDINATEPADDING;
minLatitude -= COORDINATEPADDING;
@ -128,7 +128,7 @@ public class RaceXMLReader extends XMLReader{
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
NodeList nMarks = ((Element)nCourse.item(0)).getElementsByTagName("marker");
NodeList nMarks = ((Element) nCourse.item(0)).getElementsByTagName("marker");
startPt1 = getCoordinates(nMarks, 0);
startPt2 = getCoordinates(nMarks, 0, 1);
mark = getCoordinates(nMarks, 1);
@ -140,7 +140,9 @@ public class RaceXMLReader extends XMLReader{
finishPt2 = getCoordinates(nMarks, 4, 1);
}
private Marker getMarker(NodeList start) { return getMarker(start, 0); }
private Marker getMarker(NodeList start) {
return getMarker(start, 0);
}
private Marker getMarker(NodeList start, int startIndex) {
return getMarker(start, startIndex, 0);
@ -156,7 +158,7 @@ public class RaceXMLReader extends XMLReader{
NodeList nCoordinates = markerNode.getElementsByTagName("coordinate");
GPSCoordinate side1 = getCoordinates((Element) nCoordinates.item(0));
GPSCoordinate side1 = getCoordinates((Element) nCoordinates.item(0));
GPSCoordinate side2;
if (nCoordinates.getLength() > 1) {
side2 = getCoordinates((Element) nCoordinates.item(1));
@ -167,15 +169,15 @@ public class RaceXMLReader extends XMLReader{
}
private GPSCoordinate getCoordinates(NodeList start){
private GPSCoordinate getCoordinates(NodeList start) {
return getCoordinates(start, 0);
}
private GPSCoordinate getCoordinates(NodeList start, int startIndex){
private GPSCoordinate getCoordinates(NodeList start, int startIndex) {
return getCoordinates(start, startIndex, 0);
}
private GPSCoordinate getCoordinates(NodeList start, int startIndex, int nodeIndex){
private GPSCoordinate getCoordinates(NodeList start, int startIndex, int nodeIndex) {
NodeList nodeList = ((Element) start.item(startIndex)).getElementsByTagName("coordinate");
Element coord = (Element) nodeList.item(nodeIndex);
return getCoordinates(coord);
@ -183,10 +185,11 @@ public class RaceXMLReader extends XMLReader{
/**
* Returns the coordinate TODO raise exception that runs when the XML is formatted wrongly.
*
* @param coordNode
* @return
*/
private GPSCoordinate getCoordinates(Element coordNode){
private GPSCoordinate getCoordinates(Element coordNode) {
double startLat = Double.parseDouble(getTextValueOfNode(coordNode, "latitude"));
double startLong = Double.parseDouble(getTextValueOfNode(coordNode, "longitude"));

@ -1,10 +1,14 @@
package seng302;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by fwy13 on 26/03/2017.
@ -21,7 +25,7 @@ public abstract class XMLReader {
doc.getDocumentElement().normalize();
}
public Document getDocument(){
public Document getDocument() {
return doc;
}
@ -29,7 +33,7 @@ public abstract class XMLReader {
return n.getElementsByTagName(tagName).item(0).getTextContent();
}
public String getAttribute(Element n, String attr){
public String getAttribute(Element n, String attr) {
return n.getAttribute(attr);
}

@ -76,7 +76,7 @@
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="77.0" prefWidth="200.0">
<children>
<CheckBox fx:id="showFPS" mnemonicParsing="false" selected="true" text="Show FPS" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<CheckBox fx:id="showAnno" mnemonicParsing="false" selected="true" text="Show Annotation" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="30.0" />
<CheckBox fx:id="showAnnotations" mnemonicParsing="false" selected="true" text="Show Annotation" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="30.0" />
</children>
</AnchorPane>
</content>

Loading…
Cancel
Save