0

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!

Tergo
  • 1
  • 1
  • 1
    Have you considered [drawing your shapes directly in an image](http://stackoverflow.com/a/19621211/525036) or [painting your components to an image](http://stackoverflow.com/a/11273020/525036) instead of using `Robot`? Also, make sure you are respecting the Swing Threading model (Event Dispatch Thead). – Didier L Dec 17 '15 at 17:35
  • Wow, i didn't even know that was possible! Thanks alot, that solves many of my problems! – Tergo Dec 17 '15 at 18:00

0 Answers0