- Created ResizableRaceCanvas Class - Moved all draw functions to ResizableRaceCanvas #fixmain
parent
43c84cb677
commit
bc4701ef32
@ -0,0 +1,117 @@
|
||||
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, Paint paint){
|
||||
rotate(10, 30,30);
|
||||
gc.fillPolygon(new double[]{0, 6, 12, 8, 8, 4, 4},
|
||||
new double[]{15, 0, 15, 15, 40, 40, 15},
|
||||
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 = map.convertGPS(32.296577, -64.854304);
|
||||
displayBoat(boat1coord, Color.AQUAMARINE);
|
||||
//finish line
|
||||
gc.setLineWidth(2);
|
||||
GraphCoordinate finishLineCoord1 = map.convertGPS(32.317379, -64.839291);
|
||||
GraphCoordinate finishLineCoord2 = map.convertGPS(32.317257, -64.836260);
|
||||
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
|
||||
//marks
|
||||
GraphCoordinate markCoord = map.convertGPS(32.293039, -64.843983);
|
||||
GraphCoordinate southGate1 = map.convertGPS(32.284680, -64.850045);
|
||||
GraphCoordinate southGate2 = map.convertGPS(32.280164, -64.847591);
|
||||
GraphCoordinate northGate1 = map.convertGPS(32.309693, -64.835249);
|
||||
GraphCoordinate northGate2 = 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 = map.convertGPS(32.296577, -64.854304);
|
||||
GraphCoordinate startline2 = map.convertGPS(32.293771, -64.855242);
|
||||
displayLine(startline1, startline2, Color.GREEN);
|
||||
|
||||
displayArrow(new GraphCoordinate(100, 100), Color.BLUEVIOLET);
|
||||
}
|
||||
|
||||
@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