Swing + DoubleBuffering

Hello Everyone,

On start i want to say big hello to all JavaGaming Forum members.
This is my first post on that forum i try to seek answer on existing posts but only i have big trash in head :slight_smile:

I read that Swing user automatic DoubleBuffering method but when i put

System.Out.Println(parentWindow.isDoubleBuffered()) i have answer “false”, ofcourse parentWindows is JFrame object. i try use manualy DB but only have bad resultats on it.

I interesting if somewhere are good soultion to set manualy DoubleBuffer to starting project or how to trun on DB to swing components.

Sorry for my english.

Request.

Hi and welcome!

What do you want double-buffering for? a game or just for an ordinary program?

Also, what version of java are you using? java 6 update 10 is the newest and best…

HI :slight_smile:

Yes i want Double-buffering for my game i finish write my first “PONG GAME” and my “ball” dont have smooth move on the screen like a same “Player Pad” and “AI Pad” dont have smooth move too. My Game based on JFrame windows and use simple repaint() method in game loop.

I read solusion for that is Double-Buffering, my java:

C:\Users\Quim>java -version
java version “1.6.0_07”
Java™ SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot™ Client VM (build 10.0-b23, mixed mode, sharing)

Ah ok, well you probably don’t want to use swing so much. You see repaint() schedules a paint event on swing’s thread, not in the thread you called the method in. so you could go repaint 10 times and nothing will happen straight away…

What you want to do is just get a JFrame with a custom JComponent inside it, or a Canvas, and make your own render method that paints the player etc to a VolatileImage, then paints that image to the screen.

Look around for some demos of a normal java2d game. Kev Glass’s coke and code would be a good starting point, he’s got some good tutorials. (www.cokeandcode.com)

OK i reading something about Your solution at http://www.cokeandcode.com/ and i trying to put some code to my own project, can You check if i good thinking about it or if i go to good way.


package dbtest;


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class DBTest extends Canvas{
	
         private BufferStrategy strategy;
         private boolean gameRunning = true;
         private JFrame container;
         
         private int screenWidth  = 1024;
         private int screenHeight = 764; 
         
         private int x = 1;
         
	public DBTest()
        {
           
		container = new JFrame("Space Invaders 101");
		
		JPanel panel = (JPanel) container.getContentPane();
		panel.setPreferredSize(new Dimension(screenWidth,screenHeight));
		panel.setLayout(null);
		
		setBounds(0,0,screenWidth,screenHeight);
		panel.add(this);
		

		setIgnoreRepaint(true);
		

		container.pack();
		container.setResizable(false);
		container.setVisible(true);
		

		container.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
                                
			}
                       
		});
		
		
		requestFocus();


		createBufferStrategy(2);
		strategy = getBufferStrategy();
		
	
        }
	
        public void gameLoop() {
		long lastLoopTime = System.currentTimeMillis();
		
		while (gameRunning) {
			

			long delta = System.currentTimeMillis() - lastLoopTime;
			lastLoopTime = System.currentTimeMillis();
			
			

			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
			g.setColor(Color.black);
			g.fillRect(0,0,screenWidth,screenHeight);
			
                        update();
                        paint(g);
                        
			g.dispose();
			strategy.show();
			
			try { Thread.sleep(10); } catch (Exception e) {}
		}
	}
        
        public void update()
        {
           x++;
        }
        
        public void paint(Graphics2D g)
        {
             g.setColor(Color.white);
             g.fillOval(x, getHeight() / 2, 100 , 100);
        }

	
	
	public static void main(String[] args){
	      DBTest game = new DBTest();
              
              game.gameLoop();
	}
}


in Paint method i test “Oval” to smooth move to the right side of screen. Method isDoubleBuffered still say “false” in System Log but meybe i put that check in wrong place.

Thanks you for Your help, friend.