java - Regulating the number of executions per second using JavaFX -


so of right i'm implementing conway's game of life using javafx. in nutshell, in class extending animationtimer, within handle() method, traverses through every cell in 2d array , updates each position, draws canvas using information in 2d array.

this works fine problem runs far fast. can't see what's going on on canvas. on window have canvas few buttons. added thread.sleep(1000) try regulate generation/frame per second, doing causes window not detect button presses immediately. button presses responsive when not telling thread sleep.

does have suggestions on how solve this?

you can use timeline more suitable this. set cycle count animation.indefinite, , add keyframe delay want between updates, , current handle implementation 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, may try have delay of keyframe zero, , use timeline's targetframerate, never tried it.

edit: option keep frameskip variable in animationtimer:

private int frameskip = 0; private final int skip = 10;  @override public void handle(long now) {     frameskip++;     if (frameskip <= skip) {         // nothing, wait next frame;         return;     }     // every skip frames, reset frameskip , animation     frameskip = 0;      // animation... } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -