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

Improvements for 2022

Use java 17.
Use AnimationTimer instead of Thread.
parent 4d4c762b
Branches
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>11</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.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
......@@ -19,6 +20,8 @@ public class App extends Application {
private Canvas canvas;
private AnimationTimer timer;
@Override
public void start(Stage primaryStage) {
try {
......@@ -35,22 +38,14 @@ public class App extends Application {
//Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram);
timer = new DrawingThread(canvas);
timer.start();
//Draw scene on a separate thread to avoid blocking UI.
new Thread(this::drawScene).start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Draws objects into the canvas. Put you code here.
*
*@return nothing
*/
private void drawScene() {
new Laboratory().draw(canvas);
}
private void exitProgram(WindowEvent evt) {
System.exit(0);
......
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) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
this.world = new World(canvas);
}
/**
* 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();
if (lasttime > 0) {
//time are in nanoseconds and method simulate expects miliseconds
world.simulate((now - lasttime) / 1e6);
}
lasttime = now;
}
}
package lab;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class Laboratory {
public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
World world = new World(canvas.getWidth(), canvas.getHeight());
while (!Routines.isEndOfThreadRequestedByJavaVM()) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
world.draw(canvas);
Routines.sleep(25);
world.simulate(25);
}
}
}
......@@ -5,49 +5,33 @@ 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(double width, double height) {
public World(Canvas canvas) {
super();
this.width = width;
this.height = height;
this.canvas = canvas;
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(), height - worldPoint.getY());
return new Point2D(worldPoint.getX(), canvas.getHeight() - worldPoint.getY());
}
public void draw(Canvas canvas) {
public void draw() {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
cannon.draw(gc);
bulletAnimatted.draw(gc);
}
public void simulate(int timeDelta) {
public void simulate(double timeDelta) {
bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = 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