Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Render methods  (Read 1095 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS
« Posted 2013-07-09 14:36:43 »

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) {
   Graphics g = bs.getDrawGraphics();
   // render stuff

I stumbled upon another guide recently using, in my eyes, a more understandable method:
public void paint (Graphics 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!

Offline PaidGEEK

Senior Devvie

Medals: 5
Projects: 3

« Reply #1 - Posted 2013-07-09 14:52:25 »

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.

Improvisational programmer.
Offline VaPS
« Reply #2 - Posted 2013-07-09 14:57:59 »

Thanks a bunch!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-07-09 21:44:47 »

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 :/

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 GraphicsDevice.createCompatibleImage()

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.

Offline SHC
« Reply #4 - Posted 2013-07-10 02:12:04 »


I want to know what operations you exactly did to increase the performance. Can I use
in applet?

Offline kpars

JGO Kernel

Medals: 146
Projects: 3
Exp: 4 years

// No Comment.

« Reply #5 - Posted 2013-07-10 02:29:57 »

// 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.

    Check out my website!  |   Use the Mercury game library!   |  Follow me on Twitter!      |          |

Offline GabrielBailey74
« Reply #6 - Posted 2013-07-10 02:33:18 »

I want to know what operations you exactly did to increase the performance. Can I use
in applet?

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
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:

Offline SHC
« Reply #7 - Posted 2013-07-10 03:03:53 »


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

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

deepthought (47 views)
2015-06-30 15:39:44

deepthought (50 views)
2015-06-30 15:39:09

deepthought (62 views)
2015-06-30 15:36:52

Za\'Anzabar (37 views)
2015-06-29 05:44:54

TritonDreyja (47 views)
2015-06-24 17:10:40

CopyableCougar4 (46 views)
2015-06-23 00:34:45

BurntPizza (52 views)
2015-06-21 20:36:46

cookiecompiler (93 views)
2015-06-11 15:42:53

cookiecompiler (55 views)
2015-06-11 15:41:14

NegativeZero (80 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!