Render methods

I’m trying to create my first game in Java and I’ve seen most tutorials using a render method along the following lines:

public void render () {
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3);
		return;
	}
	Graphics g = bs.getDrawGraphics();
	g.drawImage(image,0,0,getWidth(),getHeight(),null);
	// render stuff
	g.dispose();
	bs.show();
}

I stumbled upon another guide recently using, in my eyes, a more understandable method:

public void paint (Graphics g) {
	super.paint(g);
	Graphics2d g2d = (Graphics2D) g;
	// render stuff
	// repaint() is then used to repaint the window
}

I’m leaning toward this being a case of performance. I too noticed the first paragraph of code, the one I currently use, is with JFrame and the latter one with JPanel. Which one would you recommend?

Thanks for the help!

Use the code in first paragraph, because that’s how you use double buffering (triple in your case). It draws an offscreen image and then draws that image to screen, without drawing other stuff separately, which removes flickering in java2D games.

Thanks a bunch!

Last night was the first time I ever implemented rendering via a BufferStrategy and let me say I’ll never regular render lol.

I was rendering a 1024x600 (Image file: 1920, 1200) via simply overriding the JPanels paint method and I was only pulling around 20 FPS :confused:

I couldn’t stand having a low FPS so I Googled for performance tuning 2D and I found 5-7 useful hints on 1 page and some of them were:
Use a BufferStrategy (Via a canvas)
Use [i]GraphicsDevice.createCompatibleImage/i
etc

Anyhow, I switched to rendering the chunky Image on a double buffered BufferStrategy and the rest via Graphics2D (Super RenderingHints) and my FPS is now around 60-90.

@GabrielBailey74

I want to know what operations you exactly did to increase the performance. Can I use [icode]GraphicsDevice.createCompatibleImage()[/icode] in applet?


// Make sure the class extends applet.
Image display;

run() {
    display = createVolatileImage(800, 600);
}

Here’s how I boost performance.
If you want my basic boilerplate code I use for most games, look at that here. if that helps.

I don’t know about applets mate, I switched from overriding the JPanels paint(Graphics g) method to drawing off of the Canvas.BufferStrategy (Canvas replaces the JPanel) I have yet to implement using [icode]GraphicsDevice.createCompatibleImage()[/icode] for my images.

In my case I just achieved a great performance increase because I was double buffering the chunky Image VS. rendering it with fancy graphics.

See here for “fancy graphics” layout: http://www.java-gaming.org/index.php?topic=27079.0

@kpars

I’ve modified your code to add fps counter and the result is awesome, 698 fps on my system. Here’s the modified pastebin.

http://pastebin.java-gaming.org/d314375486e