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

Use Java version 17.

Use AnimationTimer instead of common Thread.
parent ed4b8024
Branches
No related merge requests found
...@@ -8,19 +8,19 @@ ...@@ -8,19 +8,19 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId> <artifactId>javafx-controls</artifactId>
<version>11</version> <version>17.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>11</version> <version>17.0.2</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency> <dependency>
......
package lab; package lab;
import javafx.animation.AnimationTimer;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
...@@ -20,6 +20,7 @@ public class App extends Application { ...@@ -20,6 +20,7 @@ public class App extends Application {
} }
private Canvas canvas; private Canvas canvas;
private AnimationTimer timer;
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
...@@ -37,25 +38,19 @@ public class App extends Application { ...@@ -37,25 +38,19 @@ public class App extends Application {
//Exit program when main window is closed //Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram); primaryStage.setOnCloseRequest(this::exitProgram);
//graphic context is used for a painting
//Draw scene on a separate thread to avoid blocking UI. GraphicsContext gc = canvas.getGraphicsContext2D();
new Thread(this::drawScene).start();
timer = new DrawingThread(gc);
timer.start();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override
/** public void stop() throws Exception {
* Draws objects into the canvas. Put you code here. timer.stop();
* super.stop();
*@return nothing
*/
private void drawScene() {
//graphic context is used for a painting
GraphicsContext gc = canvas.getGraphicsContext2D();
// put your code here
// gc.setFill(Color.AQUA);
// gc.setStroke(Color.BLACK);
} }
private void exitProgram(WindowEvent evt) { private void exitProgram(WindowEvent evt) {
......
package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingThread extends AnimationTimer {
private final GraphicsContext gc;
public DrawingThread(GraphicsContext gc) {
this.gc = gc;
}
/**
* Draws objects into the canvas. Put you code here.
*/
@Override
public void handle(long now) {
// put your code here
gc.setFill(Color.AQUA);
gc.setStroke(Color.BLACK);
gc.fillOval(10, 10, 20, 20);
}
}
module lab01 { module lab01 {
requires transitive javafx.controls; requires transitive javafx.controls;
requires javafx.fxml; requires javafx.fxml;
requires javafx.base;
opens lab to javafx.fxml; opens lab to javafx.fxml;
exports lab; exports lab;
} }
\ No newline at end of file
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