I am trying to create a custom Canvas widget under JavaFX, which gathers information from a lot of unrelated working threads. The "update" events might come in various rates, sometimes too fast and sometimes none at all, meaning that there is a need to properly address a "too fast update" and an "idle time" requirement. The number of updates is too high, so emitting a Runnable every time an update is performed is not a good solution.
Just for testing, my brute force approach was to create an AnimationTimer to update the visuals of the Canvas object. This solution works fine, but of course there are times that the Canvas doesn't need to be updated, making the whole procedure inefficient.
The closest answer to my problem seems to be this post with a GUIUpdater helper class, but Canvas seems not to have a direct update property. I am trying to find a way to "plug" the needed update functionality with the binded parameters, but with luck. I tried even to understand if Canvas has an "update" event, but I didn't find it. All other answers I found didn't meet the requirements - as far as I understand.
Thanks in advance
EDIT
Here is the code I use to make sure that only if the data changes, then the canvas will be drawn:
new AnimationTimer() {
@Override
public void handle(long now) {
if (needsUpdate.getAndSet(false)) {
myCustomCanvasRepaint();
}
}
}.start();
which is triggered with something like:
AtomicBoolean needsUpdate = new AtomicBoolean(true);
...
public void needsUpdate() {
needsUpdate.set(true);
}