Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I have a JFrame holding a JPanel, to which I'm drawing at about 60 FPS. I've been told that "Swing is double buffered by default", but, nevertheless I'm getting massive flickering. At first, I tried modifying the Graphics object from JPanel.getGraphics(), but was not surprised when it flickered (to black, the background colour of the game so far), as I assumed that it was redrawing when I blanked the screen. However, then I overrode paintComponent(), passing the Graphics to all objects to draw with, and made it call the super version, which I assumed would solve it, but no difference.

I'm still getting the Graphics object from the same place, i.e. JPanel.getGraphics(), because paintComponent() needs a Graphics argument. Should I be getting it from some other place? If not, what else could be causing the problem?

share|improve this question

2 Answers 2

tried to activate double buffering manually (just to make sure)? setDoubleBuffered

On the other hand I always used a self writen double buffering at the (overwritten) update method. Something like that

share|improve this answer
    
+1 I too manually wrote double buffering. –  pek Oct 19 '11 at 1:18
    
This is a good answer, but it turns out my problem was what method I was calling in the first place. But either way, useful info, thanks. –  AlbeyAmakiir Oct 19 '11 at 7:35
up vote 1 down vote accepted

Whoops. Looks like I should have been calling repaint(), not calling paintComponent(Graphics) directly. repaint() calls that as part of the double buffering process, so it shouldn't be called by hand if you want to avoid flickering.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.