r/JavaFX • u/FrameXX • Mar 24 '25
Help How to render circles on WritableImage without such glitches?
I am rendering circles on a WritableImage using PixelBuffer and IntBuffer and then showing the WritableImage in an ImageView, but for some reason I get these periodic glitches. The glitches seem to be bound to how often I update the image. If I set the FPS cap to a higher value the glitch period shortens. The glitching is also bound to how big the circles are on the screen. If I zoom out enough so that the circles are smaller the glitching disappears.
Here's more on how I render the circles:
I set values in the IntBuffer in one thread that's separate from the main JavaFX thread using ExecutorService. On the main thread I periodically (using ScheduledExecutorService) update the PixelBuffer of the WritableImage and set the view with the WritableImage.
I don't create WritableImage for every new frame instead I have a buffer of frames that I reuse and that are shared among the 2 threads.
private final freshFrames;
private final oldFrames;
I don't know if this could be a problem, becuase in the example I took inspiration from a new WritableImage is created for every update from a PixelBuffer and only PixelBuffers are shared among the threads.
I uploaded the code on Github for more details: https://github.com/FrameXX/particle-life
I would be thankful for any help, especially someone more experienced in this kind of stuff. I am propably just doing something dumb.
View all comments
Show parent comment
3
u/john16384 Mar 25 '25 edited Mar 25 '25
I've checked your code. There is something wrong in your frame picking logic. I logged what frame was picked by
updateImagefor actual display, and what frame was picked next for rendering. They were the same.As you can see, handle is displaying frame X, and immediately frame X is also picked for rendering each time...
I changed
updateImageto this:Where
displayedFrameis a field. It still flickers though, I'm looking further...Edit: figured it out; the image you just replaced (when calling
setImage) should not be re-used immediately as it may still be actively referenced for drawing the current frame. Adding some delay here removes all the flicker. Here is some modified code that works:Finally, I don't think how you are doing the rendering currently will scale that well (you're basically not using the GPU when setting pixels the way you do). I've drawn complex maps, with many lines, circles, icons, depth fading (etc) before, and I just used Canvas and its Graphics2D for this. It was flicker free out of the box, and it will use the GPU (you basically queue up a list of instructions, like clear canvas, draw circle there, draw line here). Bonus is also that these draw instructions can benefit from anti-aliasing/smoothing.