Simple question...double buffering related...

OK...I'm making a maze game and I need to double buffer a set of images...at the moment I have my player image (a ship) double buffered but the rest of my maze (which, at the moment consists of one image for all blocks) is not double buffered and flickers quite badly. No matter what I try I can't get the whole thing double buffered with out getting weird effects happening.

So I need to know how to double buffer multiple images...

here's my paint code...

int x, y;

Graphics2D g2 = (Graphics2D)g;

bufferdImgSurface.setBackground(Color.black);

// Clear the applet.

bufferdImgSurface.clearRect(0, 0, width, height);

bufferdImgSurface.drawImage(Ship, CurrentX, CurrentY,this);

g2.drawImage(bufferdImg, 0, 0,this);

// Draw Maze

for (row = 0; row < 10; row++)

{

for (col = 0; col < 10; col++)

{

if (MazeArray[row][col] == 1)

{

x = col * w;

y = row * h;

g2.drawImage(MiddleRock, x, y,this);

}

}

}

There is some other code...like declaring BufferedImage and the like if that would be helpful just say...I'm not wanting to post all of my code though.

...some global variables I declared which are related...

BufferedImage bufferdImg;

Graphics2D bufferdImgSurface;

and in my init function

bufferdImg = (BufferedImage)createImage(width, height);

bufferdImgSurface = bufferdImg.createGraphics();

[1939 byte] By [kujoswratha] at [2007-9-29 16:21:31]
# 1

yeah you didn't do the double buffering part. Here's a template I use for all my games:

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

public class Template extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener {

//Static variables

private ThreadmyBoard = null;

//Status and Game Control related variables

//display objects and variables

private Imageback;

private GraphicsbackG;

private Graphics2Dg2d;

private longnextTime;

public void init() {

back = createImage(getWidth(), getHeight());

backG = back.getGraphics();

g2d = (Graphics2D) backG;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

//add the action listeners

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

}

public void destroy() {}

public void update(Graphics g) {g.drawImage(back, 0, 0, this);}

public void paint(Graphics g) {g.drawImage(back, 0, 0, this);}

public void start() {

if (myBoard == null) {

myBoard = new Thread(this);

myBoard.start();

nextTime = System.currentTimeMillis() + 25;

}

}

public void stop() {

if (myBoard != null) {

myBoard.stop();

myBoard = null;

}

}

public void drawMap() {

}

public void run() {

Thread me = Thread.currentThread();

while (myBoard == me) {

try {

Thread.sleep(Math.max(5,nextTime-System.currentTimeMillis()));

} catch (InterruptedException e) {

e.printStackTrace();

}

nextTime=System.currentTimeMillis()+30;

drawMap();

repaint();

}

}

public void mouseDragged(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {}

public void keyPressed(KeyEvent e) {}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

Put the stuff you have in your "paint" code in "drawMap()"

Malohkana at 2007-7-15 14:38:00 > top of Java-index,Other Topics,Java Game Development...
# 2

actually for your purpose I'd make the init():

public void init() {

back = createImage(getWidth(), getHeight());

backG = back.getGraphics();

g2d = (Graphics2D) backG;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

bufferedImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);

bufferedImageG2D = bufferedImage.createGraphics();

bufferedImageG2D.setColor(Color.black);

bufferedImageG2D.fillRect(0, 0, getWidth(), getHeight());

//add the action listeners

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

}

and in drawMap()

public void drawMap() {

g2d.drawImage(bufferedImage, 0, 0, this);

//put draw ship code here

}

Malohkana at 2007-7-15 14:38:00 > top of Java-index,Other Topics,Java Game Development...
# 3
I got it fixed...my code was ok...it was how I ordered it that was wrong...I was drawing my ship then clearing the buffer then drawing the maze or some other way round...which was causing me some grief...but now it's sorted :Dthanks for your help anyway though :)
kujoswratha at 2007-7-15 14:38:00 > top of Java-index,Other Topics,Java Game Development...