I think I can show my solution best with my main loop. It will probably work for you too. I do, however, use active rendering. Repaint might conceivably still be called if you're using passive rendering, though I don't know why it would be.
Here's the code for my main loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public void execute() { shouldContinueLoop = true; ...
while(shouldContinueLoop) { if(!isApplicationActive()) { try { Thread.sleep(500); } catch(InterruptedException exception) {} continue; }
... } } |
This isApplicationActive method determines whether the application is "active". All it does is call Window.isActive() for the main Window of the game. The window is inactive when another application is selected and presumably also when the window is minimized.
Sleeping for half a second (or whatever amount of time you like) is necessary to prevent your program from taking up CPU cycles by checking whether the window is active constantly.