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

Add drawingthread

parent d08b5f8f
No related merge requests found
package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class DrawingThread extends AnimationTimer {
private final Canvas canvas;
private final GraphicsContext gc;
private final World world;
private long lasttime = -1;
public DrawingThread(Canvas canvas, World world) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
this.world = world;
}
/**
* Draws objects into the canvas. Put you code here.
*/
@Override
public void handle(long now) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
world.draw(gc);
if (lasttime > 0) {
//time are in nanoseconds and method simulate expects seconds
world.simulate((now - lasttime) / 1e9);
}
lasttime = now;
}
}
...@@ -34,19 +34,7 @@ public class GameController { ...@@ -34,19 +34,7 @@ public class GameController {
public void startGame() { public void startGame() {
this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world = new World(canvas.getWidth(), canvas.getHeight());
//Draw scene on a separate thread to avoid blocking UI. //Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimer() { animationTimer = new DrawingThread(canvas, world);
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
};
angleSlider.valueProperty().addListener(this::angleChanged); angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue()); world.setCannonAngle(angleSlider.getValue());
...@@ -60,11 +48,6 @@ public class GameController { ...@@ -60,11 +48,6 @@ public class GameController {
animationTimer.stop(); animationTimer.stop();
} }
private void drawScene(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
@FXML @FXML
private void firePressed() { private void firePressed() {
world.fire(); world.fire();
......
...@@ -3,7 +3,6 @@ package lab; ...@@ -3,7 +3,6 @@ package lab;
import java.util.Random; import java.util.Random;
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 {
...@@ -44,9 +43,8 @@ public class World { ...@@ -44,9 +43,8 @@ public class World {
return new Point2D(worldPoint.getX(), height - worldPoint.getY()); return new Point2D(worldPoint.getX(), height - worldPoint.getY());
} }
public void draw(Canvas canvas) { public void draw(GraphicsContext gc) {
GraphicsContext gc = canvas.getGraphicsContext2D(); gc.clearRect(0, 0, getWidth(), getHeight());
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
for(DrawableSimulable entity: entities) { for(DrawableSimulable entity: entities) {
entity.draw(gc); entity.draw(gc);
} }
......
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