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

Refactor and use AnimationTimer.

parent 0f74c9f3
No related merge requests found
......@@ -19,8 +19,9 @@ public class App extends Application {
}
private Canvas canvas;
private AnimationTimer animationTimer;
private Laboratory lab;
private AnimationTimer timer;
@Override
public void start(Stage primaryStage) {
try {
......@@ -32,44 +33,21 @@ public class App extends Application {
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 3rd laboratory");
primaryStage.setTitle("Java 1 - 4th laboratory");
primaryStage.show();
lab = new Laboratory(canvas);
//Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimer() {
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
};
animationTimer.start();
//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.
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Draws objects into the canvas. Put you code here.
*
*@return nothing
*/
private void drawScene(double deltaT) {
lab.draw(deltaT);
}
private void exitProgram(WindowEvent evt) {
animationTimer.stop();
System.exit(0);
}
}
\ No newline at end of file
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.getWidth(), canvas.getHeight());
}
/**
* 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;
}
}
package lab;
import javafx.scene.canvas.Canvas;
public class Laboratory {
private World world;
private Canvas canvas;
public Laboratory(Canvas canvas) {
this.canvas = canvas;
this.world = new World(canvas.getWidth(), canvas.getHeight());
}
public void draw(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class World {
......@@ -26,9 +25,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, width, height);
cannon.draw(gc);
bulletAnimatted.draw(gc);
for(Dragon dragon: dragons) {
......
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