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
2
u/sedj601 Mar 25 '25
If you are drawing or animating stuff, use something from the Animation API. That's what it's designed for. Timeline is popular, and AnimationTimer.
Here are some examples:
https://stackoverflow.com/a/54928375/2423906
https://stackoverflow.com/a/53174305/2423906
https://stackoverflow.com/a/56503657/2423906
https://stackoverflow.com/a/78122438/2423906
https://stackoverflow.com/a/49057643/2423906