I am trying to repaint and capture an image of a JPanel multiple times per second, but have been unable to do so effectivly. What ends up happening so far is that it runs smooth at first, but after a few seconds it starts to lag. This causes identical images to be captured.
So far my code looks like this:
BufferedImage contentsImage;
visualizer.setData(approximation);
visualizer.repaint();
Rectangle bounds = new Rectangle(visualizer.master.getRootPane().getSize());
bounds.setLocation(visualizer.master.getRootPane().getLocationOnScreen());
contentsImage = (new Robot()).createScreenCapture(bounds);
So what i'm trying to do here is that visualizer is my JPanel which gets a bunch of primitive shapes from approximation with setData(). These shapes are then painted by repaint().
From searching i know that repaint is not necessarily instantaneous, and i'm guess that's why it starts to "lag", because it's grouping the repaint() calls together.
How should i attempt this problem? I've read that timers could possibly work, but i don't understand how to tie the timer, repaint and capture screen together nicely. I only want to capture the image after the correct primitive shapes has been painted.
I know there will be performance issues, but those i'll be able to handle later on. I want to get this working first. Thanks!