Java Double Buffering Panel

wondered if anyone could point me in the right guide, I developed a pong game and I need double buffering due to flickering. Iv tried part of the post here to try to get it to work, but I'm still new to awt swings, any help would be awesome.

public class PongPanel extends JPanel implements Runnable { private int screenWidth = 500; private int screenHeight = 300; private boolean isPaused = false; private boolean isGameOver = false; private int playToPoints = 10; private Padel player1,player2; private Ball ball; private Thread gameThread; private Image dbImage; private Graphics dbg; public PongPanel() { setPreferredSize(new Dimension(screenWidth,screenHeight)); setBackground(Color.BLACK); setDoubleBuffered(true); setFocusable(true); requestFocus(); player1 = new Padel(Position.LEFT,screenWidth,screenHeight); player2 = new Padel(Position.RIGHT,screenWidth,screenHeight); ball = new Ball(10,screenWidth/2,screenHeight/2,Color.WHITE); } public void addNotify(){ super.addNotify(); startGame(); } private void startGame(){ gameThread = new Thread(this); gameThread.start(); } @Override public void run() { while (!isGameOver) { dbImage = createImage(screenWidth,screenHeight); dbg = this.getGraphics(); if(!isPaused){ if(!gameOverCheck()){ updateGame(); paintComponents(dbg); } }else if(isPaused){ dbg.setColor(Color.ORANGE); dbg.setFont(new Font("serif",Font.BOLD,50)); dbg.drawString("Paused", screenWidth/2-82, screenHeight/2); } try { Thread.sleep(30); } catch (InterruptedException e) {e.printStackTrace();} } } private boolean gameOverCheck(){ if(player1.getScore() == playToPoints){ dbg.setColor(player1.getColour()); dbg.setFont(new Font("serif",Font.BOLD,50)); dbg.drawString("Player 1 Wins!", screenWidth/2 - 161, screenHeight/2); setGameOver(true); return true; }else if(player2.getScore() == playToPoints){ dbg.setColor(player2.getColour()); dbg.setFont(new Font("serif",Font.BOLD,50)); dbg.drawString("Player 2 Wins!", screenWidth/2 - 161, screenHeight/2); setGameOver(true); return true; } return false; } private void updateGame(){ ball.move(screenWidth,screenHeight,player1,player2); player1.aiForPadel(screenWidth, screenHeight, ball.getX(), ball.getY()); player2.aiForPadel(screenWidth, screenHeight, ball.getX(), ball.getY()); } @Override public void paintComponents(Graphics g) { super.paintComponents(g); dbg.setColor(Color.BLACK); dbg.fillRect(0, 0, screenWidth+20, screenHeight+20); dbg.setColor(Color.WHITE); dbg.drawLine(screenWidth/2, 0, screenWidth/2, screenHeight); dbg.setFont(new Font("serif",Font.BOLD,32)); dbg.drawString(player1.getScore()+"", screenWidth/2-40, screenHeight - 20); dbg.drawString(player2.getScore()+"", screenWidth/2+20, screenHeight - 20); ball.drawBall(dbg); player1.drawPadel(dbg); player2.drawPadel(dbg); } } 
+4
source share
3 answers

There's a really good tutorial here that describes how to use BufferStrategy to create non-flickering animations.

Important points:

  • Call setIgnoreRepaint(true) at the top level of the Canvas to prevent AWT from being redrawn, as you will usually do this yourself in the animation loop.
  • Get a Graphics2D object from BufferStrategy (instead of using the instance passed through paintComponent(Graphics g) ).
+4
source

Must Read About the Drawing Engine in AWT and Swing

Painting in AWT and Swing

+1
source

I think you can just call super(true); the first thing, and it just tells JPanel that it is buffered twice ... because one of the JPanel constructors is:

 public Jpanel(boolean isDoubleBuffered) {...} 

I hope this helps someone, although it's almost four years later.

-one
source

All Articles