I am writing a simple Java Swing application designed as a demo for a course. The goal of the program is to visualize the course of the recursive method (in particular, the recursive solution for the maze). I would like to be able to update the appearance of the JComponent as the recursive method is executed, so from the point of view of Swing, updates cannot be launched separately from the Timer class that I am building, this is a normal animation method.
My first thought was to just call repaint () when I want to redraw the visualization. But the recursive method itself is called in the JButton event handler, which means that all repaint () calls are delayed until the event handler completes - and all I see is the last frame of the animation. I found that I can get the drawing immediately if I call update () instead, for example:
getRootPane().getContentPane().update(getGraphics()); try { Thread.sleep(25); } catch (Exception e) { }
It almost works, except the animation flickers. I know Swing has a double buffer, but apparently getGraphics gives me the active buffer, not the back buffer. I do not see any methods that would give me access to the feedback buffer.
So, I'm looking for a solution that will allow me to redraw the component in the middle of the recursion and give me flicker-free animation. Is there a way to activate built-in double buffering during the execution of an event handler? Would it be better to try to implement my own buffering system? Or is this what I'm trying to make simply impossible / advisable with Swing?
(As a final note, this is my first post here. I tried to write a good question, but if I missed something, please let me know beautifully and I will try to do better next time.!)
source share