commit
b3b200fb02
@ -0,0 +1,121 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
import javafx.scene.canvas.Canvas;
|
||||||
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import javafx.scene.paint.Paint;
|
||||||
|
import javafx.scene.transform.Rotate;
|
||||||
|
import seng302.GraphCoordinate;
|
||||||
|
import seng302.RaceMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by fwy13 on 17/03/17.
|
||||||
|
*/
|
||||||
|
public class ResizableRaceCanvas extends Canvas {
|
||||||
|
GraphicsContext gc;
|
||||||
|
RaceMap map;
|
||||||
|
|
||||||
|
public ResizableRaceCanvas(RaceMap map) {
|
||||||
|
this.map = map;
|
||||||
|
gc = this.getGraphicsContext2D();
|
||||||
|
// Redraw canvas when size changes.
|
||||||
|
widthProperty().addListener(evt -> draw());
|
||||||
|
heightProperty().addListener(evt -> draw());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResizableRaceCanvas(){
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMap (RaceMap map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void displayBoat(GraphCoordinate graphCoordinate, Paint paint){
|
||||||
|
gc.setFill(paint);
|
||||||
|
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 15, 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void displayLine(GraphCoordinate graphCoordinateA, GraphCoordinate graphCoordinateB, Paint paint){
|
||||||
|
gc.setStroke(paint);
|
||||||
|
gc.setFill(paint);
|
||||||
|
gc.fillOval(graphCoordinateA.getX() - 3, graphCoordinateA.getY() - 3, 6, 6);
|
||||||
|
gc.fillOval(graphCoordinateB.getX() - 3, graphCoordinateB.getY() - 3, 6, 6);
|
||||||
|
gc.strokeLine(graphCoordinateA.getX(), graphCoordinateA.getY(), graphCoordinateB.getX(), graphCoordinateB.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void displayPoint(GraphCoordinate graphCoordinate, Paint paint){
|
||||||
|
gc.setFill(paint);
|
||||||
|
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 10, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void displayArrow(GraphCoordinate coordinate, int angle){
|
||||||
|
gc.save();
|
||||||
|
rotate(angle, coordinate.getX(),coordinate.getY());
|
||||||
|
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);
|
||||||
|
gc.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void rotate(double angle, double px, double py) {
|
||||||
|
Rotate r = new Rotate(angle, px, py);
|
||||||
|
gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw() {
|
||||||
|
double width = getWidth();
|
||||||
|
double height = getHeight();
|
||||||
|
System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height);
|
||||||
|
this.map = new RaceMap(32.321989, -64.873, 32.28, -64.831, (int)width, (int)height);
|
||||||
|
|
||||||
|
if (map == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println("Drawing");
|
||||||
|
gc.clearRect(0, 0, width, height);
|
||||||
|
//boat
|
||||||
|
GraphCoordinate boat1coord = this.map.convertGPS(32.296577, -64.854304);
|
||||||
|
displayBoat(boat1coord, Color.AQUAMARINE);
|
||||||
|
//finish line
|
||||||
|
gc.setLineWidth(2);
|
||||||
|
GraphCoordinate finishLineCoord1 = this.map.convertGPS(32.317379, -64.839291);
|
||||||
|
GraphCoordinate finishLineCoord2 = this.map.convertGPS(32.317257, -64.836260);
|
||||||
|
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
|
||||||
|
//marks
|
||||||
|
GraphCoordinate markCoord = this.map.convertGPS(32.293039, -64.843983);
|
||||||
|
GraphCoordinate southGate1 = this.map.convertGPS(32.284680, -64.850045);
|
||||||
|
GraphCoordinate southGate2 = this.map.convertGPS(32.280164, -64.847591);
|
||||||
|
GraphCoordinate northGate1 = this.map.convertGPS(32.309693, -64.835249);
|
||||||
|
GraphCoordinate northGate2 = this.map.convertGPS(32.308046, -64.831785);
|
||||||
|
displayBoat(boat1coord, Color.AQUAMARINE);
|
||||||
|
displayBoat(markCoord, Color.GOLD);
|
||||||
|
displayLine(southGate1, southGate2, Color.DARKCYAN);
|
||||||
|
displayLine(northGate1, northGate2, Color.DARKVIOLET);
|
||||||
|
//start line
|
||||||
|
GraphCoordinate startline1 = this.map.convertGPS(32.296577, -64.854304);
|
||||||
|
GraphCoordinate startline2 = this.map.convertGPS(32.293771, -64.855242);
|
||||||
|
displayLine(startline1, startline2, Color.GREEN);
|
||||||
|
|
||||||
|
//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
|
||||||
|
public boolean isResizable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double prefWidth(double height) {
|
||||||
|
return getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double prefHeight(double width) {
|
||||||
|
return getHeight();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue