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

Use AnimationTimer.

parent 4e818c66
No related merge requests found
package lab;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
......@@ -19,6 +19,7 @@ public class App extends Application {
}
private Canvas canvas;
private AnimationTimer timer;
@Override
public void start(Stage primaryStage) {
......@@ -36,27 +37,16 @@ public class App extends Application {
//Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram);
//Draw scene on a separate thread to avoid blocking UI.
new Thread(this::drawScene).start();
timer = new DrawingThread(canvas);
timer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Draws objects into the canvas. Put you code here.
*
*@return nothing
*/
private void drawScene() {
//graphic context is used for a painting
GraphicsContext gc = canvas.getGraphicsContext2D();
int timeout = 10;
while (!Routines.isEndOfThreadRequestedByJavaVM()) {
Routines.sleep(timeout);
double deltaT = timeout / 1000.;
}
@Override
public void stop() throws Exception {
timer.stop();
super.stop();
}
private void exitProgram(WindowEvent evt) {
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Bullet {
private World world;
private Point2D velocity;
private double size;
private Point2D position;
public Bullet(World world, Point2D startPosition, Point2D velocity, double size) {
super();
this.world = world;
this.velocity = velocity;
this.size = size;
this.position = startPosition;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.BROWN);
Point2D p = world.getCanvasPoint(position);
gc.fillOval(p.getX() - size /2, p.getY() - size/2, size, size);
gc.restore();
}
public void simulate(double deltaT) {
// TODO Auto-generated method stub
position = position.add(velocity.multiply(deltaT));
}
}
package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class DrawingThread extends AnimationTimer {
private final GraphicsContext gc;
private final World world;
private long lastTime;
public DrawingThread(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) {
if (lastTime > 0) {
double deltaT = (now - lastTime) / 1e9;
world.simulate(deltaT);
}
world.draw(gc);
lastTime= now;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class World {
private double width;
private double height;
private Bullet bullet;
public World(double width, double height) {
this.width = width;
this.height = height;
this.bullet = new Bullet(this, new Point2D(10, 10), new Point2D(10, 30), 15);
}
public Point2D getCanvasPoint(Point2D in) {
Point2D result = new Point2D(in.getX(), height - in.getY());
return result;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.LIGHTGREY);
gc.fillRect(0, 0, width, height);
gc.restore();
bullet.draw(gc);
}
public void simulate(double deltaT) {
this.bullet.simulate(deltaT);
}
}
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