@ -1,7 +1,9 @@
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 ;
@ -17,6 +19,7 @@ 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 .
@ -27,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 .
@ -38,8 +44,8 @@ public class ResizableRaceCanvas extends Canvas {
this . boats = boats ;
}
public ResizableRaceCanvas ( RaceMap map ) {
super ( ) ;
this . map = map ;
gc = this . getGraphicsContext2D ( ) ;
// Redraw canvas when size changes.
@ -52,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 ( ) ) ) ;
}
/ * *
@ -72,8 +84,23 @@ public class ResizableRaceCanvas extends Canvas {
* @see 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 ) ;
}
public void displayBoat ( BoatInRace boat , double angle ) {
GraphCoordinate pos = this . map . convertGPS ( boat . getCurrentPosition ( ) ) ;
Paint paint = boat . getColour ( ) ;
double [ ] x = { pos . getX ( ) - 6 , pos . getX ( ) , pos . getX ( ) + 6 } ;
double [ ] y = { pos . getY ( ) + 12 , pos . getY ( ) - 12 , pos . getY ( ) + 12 } ;
gc . setFill ( paint ) ;
gc . fillOval ( graphCoordinate . getX ( ) , graphCoordinate . getY ( ) , 25 , 25 ) ;
gc . save ( ) ;
rotate ( angle , pos . getX ( ) , pos . getY ( ) ) ;
gc . fillPolygon ( x , y , 3 ) ;
gc . restore ( ) ;
}
/ * *
@ -85,7 +112,7 @@ public class ResizableRaceCanvas extends Canvas {
* @see Color
* @see Paint
* /
p ublic void displayLine ( GraphCoordinate graphCoordinateA , GraphCoordinate graphCoordinateB , Paint paint ) {
p rivate void displayLine ( GraphCoordinate graphCoordinateA , GraphCoordinate graphCoordinateB , Paint paint ) {
gc . setStroke ( paint ) ;
gc . setFill ( paint ) ;
gc . fillOval ( graphCoordinateA . getX ( ) - 3 , graphCoordinateA . getY ( ) - 3 , 6 , 6 ) ;
@ -101,18 +128,19 @@ public class ResizableRaceCanvas extends Canvas {
* @see Paint
* @see Color
* /
p ublic void displayPoint ( GraphCoordinate graphCoordinate , Paint paint ) {
p rivate void displayPoint ( GraphCoordinate graphCoordinate , Paint paint ) {
gc . setFill ( paint ) ;
gc . fillOval ( graphCoordinate . getX ( ) , graphCoordinate . getY ( ) , 10 , 10 ) ;
}
/ * *
* 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 ) .
* @see GraphCoordinate
* /
p ublic void displayArrow ( GraphCoordinate coordinate , int angle ) {
p rivate void displayArrow ( GraphCoordinate coordinate , int angle ) {
gc . save ( ) ;
rotate ( angle , coordinate . getX ( ) , coordinate . getY ( ) ) ;
gc . setFill ( Color . BLACK ) ;
@ -139,8 +167,8 @@ public class ResizableRaceCanvas extends Canvas {
* @param speed speed of the boat
* @param coordinate coordinate the text appears
* /
p ublic void displayText ( String name , double speed , GraphCoordinate coordinate ) {
String text = String . format ( "%s, %2$.2f knots ", name , speed ) ;
p rivate void displayText ( String name , double speed , GraphCoordinate coordinate ) {
String text = String . format ( "%s, %2$.2f kn", name , speed ) ;
//System.out.println(text.length()*7);
long xCoord = coordinate . getX ( ) + 20 ;
long yCoord = coordinate . getY ( ) ;
@ -153,6 +181,23 @@ public class ResizableRaceCanvas extends Canvas {
gc . fillText ( text , xCoord , yCoord ) ;
}
/ * *
* Draws race map with up to date data .
* /
public void update ( ) {
this . drawRaceMap ( ) ;
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
* /
@ -163,13 +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 ;
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 ) ;
@ -188,20 +235,10 @@ public class ResizableRaceCanvas extends Canvas {
displayLine ( startline1 , startline2 , Color . GREEN ) ;
updateBoats ( ) ;
if ( boats ! = null ) {
for ( BoatInRace boat : boats ) {
if ( boat ! = null ) {
// System.out.print("Drawing Boat At: " + boat.getCurrentPosition());
displayPoint ( this . map . convertGPS ( boat . getCurrentPosition ( ) ) , boat . getColour ( ) ) ;
if ( raceAnno ) {
displayText ( boat . getAbbrev ( ) , boat . getVelocity ( ) , this . map . convertGPS ( boat . getCurrentPosition ( ) ) ) ; }
}
}
}
//display wind direction arrow - specify origin point and angle
displayArrow ( new GraphCoordinate ( 500 , 20 ) , 100 ) ;
//display wind direction arrow - specify origin point and angle - angle now set to random angle
displayArrow ( new GraphCoordinate ( ( int ) getWidth ( ) - 40 , 40 ) , 150 ) ;
}
/ * *
@ -228,6 +265,48 @@ public class ResizableRaceCanvas extends Canvas {
}
}
/ * *
* Draws boats while race in progress , when leg heading is set .
* /
public void updateBoats ( ) {
if ( boats ! = null ) {
for ( BoatInRace boat : boats ) {
boolean finished = boat . getCurrentLeg ( ) . getName ( ) . equals ( "Finish" ) | | boat . getCurrentLeg ( ) . getName ( ) . equals ( "DNF" ) ;
boolean isStart = boat . isStarted ( ) ;
if ( ! finished & & isStart ) {
displayBoat ( boat , boat . calculateHeading ( ) ) ;
GraphCoordinate wakeFrom = this . map . convertGPS ( boat . getCurrentPosition ( ) ) ;
GraphCoordinate wakeTo = this . map . convertGPS ( boat . getWake ( ) ) ;
displayLine ( wakeFrom , wakeTo , boat . getColour ( ) ) ;
} else if ( ! isStart ) {
displayBoat ( boat , boat . calculateHeading ( ) ) ;
} else {
displayBoat ( boat , 0 ) ;
}
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 ) {
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 .
*
@ -260,4 +339,17 @@ 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 ( ) ) ) ;
}
}
}
}
}