Draw problems when I move/resize jFrame

Something funky seems to be going on when I try to move or resize the jFrame. It doesn’t consistently happen either, like there’s a 20% chance each time I resize or move it.

Anyhow what happens is, my application stops drawing, but everything else keeps running. Even the methods that are supposed to draw keep on running.

If it even helps, my draw loop inside the jFrame looks like this:


public void PaintGraphics()
{   
     Graphics g = strategy.getDrawGraphics();
      
     ... [methods drawing to g ] ...

     g.dispose();       
     strategy.show(); 
}

Any ideas on how to check/restore the drawing once this happens? Or how to prevent it maybe?

Sounds like an EDT problem… you’re not meant to paint Swing components outside the Event Dispatch Thread.

Since the JFrame re-sizing occurs on the EDT but your paint code runs in your own thread (I assume) they access & change the same objects at the same time and you get dead-lock. There’s a couple of solutions, some more involved than others. What you want is for the re-sizing to take place on the EDT and for your thread to pause while that takes place. Maybe queue the resizing events, not letting them happen until your game loop has finished painting, and then do SwingUtilities.invokeAndWait(new Runable(){public void run(){frame.setSize(xxxx, yyyy)}});

Of course this is easier said than done :wink: :wink:

Let us know if you need more detail.

Best wishes,
Keith

PS: the other 2 ways to do it is to run iterations of your game loop’s painting code in the EDT (using SwingUtilities invokeAndWait) - this is ‘passive’ rendering.
Or synchronize Swing painting with your game loop’s painting using some funky code - active rendering with Swing.

Humm actually, adding


setIgnoreRepaint(true);  

seemed to have fixed everything up. I was actually doing everything on the EDT already, but you set me on the right track on what to look for though,

Thanks!