Skip to content
Snippets Groups Projects
Verified Commit 555cdf03 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Pass GraphicsContext to World.

parent 9dc3f636
Branches
No related merge requests found
......@@ -17,7 +17,7 @@ public class DrawingThread extends AnimationTimer {
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
this.world = new World(canvas);
this.world = new World(canvas.getWidth(), canvas.getHeight());
}
/**
......@@ -26,7 +26,7 @@ public class DrawingThread extends AnimationTimer {
@Override
public void handle(long now) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
world.draw();
world.draw(gc);
if (lasttime > 0) {
//time are in nanoseconds and method simulate expects seconds
world.simulate((now - lasttime) / 1e9);
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class World {
private double width;
private double height;
private BulletAnimated bulletAnimatted;
private Cannon cannon;
private final Canvas canvas;
public World(Canvas canvas) {
super();
this.canvas = canvas;
public World(double width, double height) {
this.width = width;
this.height = height;
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
}
public Point2D getCanvasPoint(Point2D worldPoint) {
return new Point2D(worldPoint.getX(), canvas.getHeight() - worldPoint.getY());
return new Point2D(worldPoint.getX(), height - worldPoint.getY());
}
public void draw() {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, width, height);
cannon.draw(gc);
bulletAnimatted.draw(gc);
}
......@@ -33,11 +32,11 @@ public class World {
}
public double getWidth() {
return canvas.getWidth();
return width;
}
public double getHeight() {
return canvas.getHeight();
return height;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment