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
1
u/FrameXX Mar 25 '25 edited Mar 25 '25
I am pretty sure that
Timelineis not a fit for me, becauseHowever
WritableImagedoes not extendWritableValue. I want to use theIntBuffer->PixelBuffer->WritableImage->ImageViewway of rendering, becuase it seems to be quite performant.The
AnimationTimerworks when I do both writing the values in theIntBufferand image updating in one thread, but so doesScheduledExecutorServiceand when using it just to update thePixelBufferandImageView(Which is the stuff that touches the JavaFX nodes and has to be does in the JavaFX thread AFAIK) and write theIntBufferin a different thread I get the same problem. I guess I will just have to live with the one thread as the threading does not improve much of anything here.