- Canvas is created via layering. - Resizable canvas is now a seperate abstract class #story[782]main
parent
e409c8752c
commit
862391d7ef
@ -0,0 +1,55 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
import javafx.scene.canvas.Canvas;
|
||||||
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by fwy13 on 4/05/17.
|
||||||
|
*/
|
||||||
|
public abstract class ResizableCanvas extends Canvas {
|
||||||
|
protected GraphicsContext gc;
|
||||||
|
|
||||||
|
public ResizableCanvas(){
|
||||||
|
this.gc = this.getGraphicsContext2D();
|
||||||
|
// Redraw canvas when size changes.
|
||||||
|
widthProperty().addListener(evt -> draw());
|
||||||
|
heightProperty().addListener(evt -> draw());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract void draw();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Canvas to resizable.
|
||||||
|
*
|
||||||
|
* @return That the Canvas is resizable.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isResizable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the preferred width of the Canvas
|
||||||
|
*
|
||||||
|
* @param width of canvas
|
||||||
|
* @return Returns the width of the Canvas
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public double prefWidth(double width) {
|
||||||
|
return getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the preferred height of the Canvas
|
||||||
|
*
|
||||||
|
* @param height of canvas
|
||||||
|
* @return Returns the height of the Canvas
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public double prefHeight(double height) {
|
||||||
|
return getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,78 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import seng302.GPSCoordinate;
|
||||||
|
import seng302.GraphCoordinate;
|
||||||
|
import seng302.Mock.StreamedCourse;
|
||||||
|
import seng302.RaceDataSource;
|
||||||
|
import seng302.RaceMap;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by fwy13 on 4/05/17.
|
||||||
|
*/
|
||||||
|
public class ResizableRaceMap extends ResizableCanvas {
|
||||||
|
private RaceMap map;
|
||||||
|
RaceDataSource raceData;
|
||||||
|
private List<GPSCoordinate> raceBoundaries;
|
||||||
|
double[] xpoints = {}, ypoints = {};
|
||||||
|
|
||||||
|
public ResizableRaceMap(RaceDataSource raceData){
|
||||||
|
super();
|
||||||
|
this.raceData = raceData;
|
||||||
|
raceBoundaries = this.raceData.getBoundary();
|
||||||
|
|
||||||
|
double lat1 = raceData.getMapTopLeft().getLatitude();
|
||||||
|
double long1 = raceData.getMapTopLeft().getLongitude();
|
||||||
|
double lat2 = raceData.getMapBottomRight().getLatitude();
|
||||||
|
double long2 = raceData.getMapBottomRight().getLongitude();
|
||||||
|
setMap(new RaceMap(lat1, long1, lat2, long2, (int) getWidth(), (int) getHeight()));
|
||||||
|
//draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMap(RaceMap map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Draw boundary of the race.
|
||||||
|
*/
|
||||||
|
public void drawBoundaries() {
|
||||||
|
if (this.raceBoundaries == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gc.setFill(Color.AQUA);
|
||||||
|
setRaceBoundCoordinates();
|
||||||
|
|
||||||
|
gc.setLineWidth(1);
|
||||||
|
gc.fillPolygon(xpoints, ypoints, xpoints.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(){
|
||||||
|
|
||||||
|
double width = getWidth();
|
||||||
|
double height = getHeight();
|
||||||
|
|
||||||
|
gc.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
|
if (map == null) {
|
||||||
|
return;//TODO this should return a exception in the future
|
||||||
|
}
|
||||||
|
this.map.setHeight((int) height);
|
||||||
|
this.map.setWidth((int) width);
|
||||||
|
|
||||||
|
gc.setLineWidth(2);
|
||||||
|
drawBoundaries();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue