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/sedj601 Mar 25 '25 edited Mar 25 '25
Okay, I tried to take a better look at your code. I don't think you need renderExecutor. I think you need to research game-loop so that you can create a better game-loop/AnimationTimer. From there, do all of your work and logic in the game-loop and not with the renderExecutor thread.
I am no expert on game loops, so I could be totally off.
Here are an expert's ideas on game loops. https://carlfx.wordpress.com/2012/03/29/javafx-2-gametutorial-part-1/
This is a simple game loop. You may need a more advanced game loop.
There used to be a really good site on game loops that I used to consult. I haven't been able to find it in years. This one seems to be a good starting point. https://gameprogrammingpatterns.com/game-loop.html