Newer
Older
package lab;
import javafx.animation.AnimationTimer;
public class DrawingThread extends AnimationTimer {
private Scene scene;
private int frameCount = 0;
private int fps = 0;
private long lastTime;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
}
/**
* Draws objects into the canvas. Put you code here.
*/
@Override
public void handle(long now) {
long timeDelta = now - lastTime;
scene.draw(gc);
scene.simulate(timeDelta);
long currentSecond = now / 1_000_000_000;
if(lastSecond == currentSecond) {
frameCount ++;
} else {
lastSecond = currentSecond;
fps = frameCount;
frameCount = 0;
}
lastTime = now;
//print FPS
gc.setStroke(Color.RED);
gc.strokeText(Integer.toString(fps), 10, 30);
gc.strokeText(Double.toString(1_000_000_000d/timeDelta), 10, 50);