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 { ...@@ -17,7 +17,7 @@ public class DrawingThread extends AnimationTimer {
public DrawingThread(Canvas canvas) { public DrawingThread(Canvas canvas) {
this.canvas = canvas; this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D(); 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 { ...@@ -26,7 +26,7 @@ public class DrawingThread extends AnimationTimer {
@Override @Override
public void handle(long now) { public void handle(long now) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
world.draw(); world.draw(gc);
if (lasttime > 0) { if (lasttime > 0) {
//time are in nanoseconds and method simulate expects seconds //time are in nanoseconds and method simulate expects seconds
world.simulate((now - lasttime) / 1e9); world.simulate((now - lasttime) / 1e9);
......
package lab; package lab;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public class World { public class World {
private double width;
private double height;
private BulletAnimated bulletAnimatted; private BulletAnimated bulletAnimatted;
private Cannon cannon; private Cannon cannon;
private final Canvas canvas;
public World(double width, double height) {
public World(Canvas canvas) { this.width = width;
super(); this.height = height;
this.canvas = canvas;
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20)); 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); bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
} }
public Point2D getCanvasPoint(Point2D worldPoint) { 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() { public void draw(GraphicsContext gc) {
GraphicsContext gc = canvas.getGraphicsContext2D(); gc.clearRect(0, 0, width, height);
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
cannon.draw(gc); cannon.draw(gc);
bulletAnimatted.draw(gc); bulletAnimatted.draw(gc);
} }
...@@ -33,11 +32,11 @@ public class World { ...@@ -33,11 +32,11 @@ public class World {
} }
public double getWidth() { public double getWidth() {
return canvas.getWidth(); return width;
} }
public double getHeight() { 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