JComponent phreaker rendering in a Swing event handler

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.!)

+4
source share
2 answers

Firstly, you should not take long steps in Thread Dispatch Thread , and I assume that one of the reasons your animation is flickering.

So, I suggest starting with a thread that executes your recursive method when the button is clicked (and possibly deactivates it until the work is done).

To render the animation, you can either call myComponent.repaint() from your recursive method, or start the Swing timer (thanks @mKorbel for guessing right), which basically looks like this:

 ActionListener repaintTrigger = new ActionListener() { public void actionPerformed(ActionEvent evt) { myComponent.repaint(); } }; new Timer(25, repaintTrigger).start(); 

I recommend the latter approach, since your recursive method probably doesn't know anything about the Swing component (unless you separate it through the "ProgressListener" or something else).

It seems to you that you are not just a beginner, I just gave some tips on what to do. If you need more input, feel free to ask.

+2
source

I really don't know if this works. I would try to make it multithreaded and separate recursion from graphics. I don't know if this works in java, but it worked for me in C #.

0
source

All Articles