Regulating the number of executions per second using JavaFX

寵の児 提交于 2019-12-02 09:36:31

You can use Timeline which is probably more suitable for this. Set cycle count to Animation.INDEFINITE, and add a KeyFrame with the delay you want between updates, and your current handle implementation as the frame's onFinished.

final Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(
        new KeyFrame(
                Duration.seconds(1), 
                event -> handle()
        )
);
timeline.play();

Alternatively, you may try to have the delay of the KeyFrame as zero, and use the Timeline's targetFrameRate, but I personally never tried it.

Edit: Another option is to keep a frameSkip variable in your AnimationTimer:

private int frameSkip = 0;
private final int SKIP = 10;

@Override
public void handle(long now) {
    frameSkip++;
    if (frameSkip <= SKIP) {
        // Do nothing, wait for next frame;
        return;
    }
    // Every SKIP frames, reset frameSkip and do animation
    frameSkip = 0;

    // Do animation...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!