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

Java 17

DrawingThread.
parent 47783dfc
No related merge requests found
......@@ -8,19 +8,19 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
<version>17.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
......
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;
}
}
......@@ -35,7 +35,7 @@ public class GameController {
this.world = new World(canvas.getWidth(), canvas.getHeight());
this.world.setGameListener(new GameListenerImpl());
//Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimerImpl();
animationTimer = new DrawingThread(canvas, world);
angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue());
......@@ -49,10 +49,6 @@ public class GameController {
animationTimer.stop();
}
private void drawScene(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
@FXML
private void firePressed() {
......@@ -70,20 +66,6 @@ public class GameController {
world.setCannonStrength(newValue.doubleValue());
}
private final class AnimationTimerImpl extends AnimationTimer {
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
}
private class GameListenerImpl implements GameListener {
@Override
......
......@@ -3,7 +3,6 @@ package lab;
import java.util.Random;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class World {
......@@ -45,9 +44,8 @@ public class World {
return new Point2D(worldPoint.getX(), height - worldPoint.getY());
}
public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, getWidth(), getHeight());
for(DrawableSimulable entity: entities) {
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