I’ve been using a different method of double buffering and drawing to a jpanel, but I’d like to try using the jpanel’s native paint / paintComponent method instead.
So, I understand this is a common problem with using a jpanel’s native paint / paint component method. But some people accomplish this simply enough, and claim that it is double buffered by default. I can’t seem to figure it out. I’ve tried reading all kinds of posts and tried all kinds of methods, and I still get a slight flicker. I’m not sure if this is due to double buffering or not – it doesn’t flicker badly. Just a little bit, and just some of the time.
Here’s my current code:
JPanel’s paintComponent method:
JPanel jpanel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(currentState()!=null) currentState().draw(bufferGraphics);
g.drawImage(bufferImage, 0, 0, width * scale, height * scale, jpanel);
}
};
And then my call in the game loop:
if (currentTime - lastRender >= fpslimit) {
jpanel.repaint();
//render();
fps++;
lastRender = currentTime;
}