Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 608 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS
« Posted 2013-07-09 16: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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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:
1  
2  
3  
4  
5  
6  
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!

Offline PaidGEEK

Senior Member


Medals: 5
Projects: 3


twitter.com/PaidGEEK


« Reply #1 - Posted 2013-07-09 16: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 16: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 23: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()
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.

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

@GabrielBailey74

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

Offline kpars

JGO Wizard


Medals: 75
Projects: 4
Exp: 3 years


Radirius Software Developer


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

1  
2  
3  
4  
5  
6  
// 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 #JGO on EsperNet IRC! | Check out the MERCury 2D Java Game Library! | Also, Check out My Site
Offline GabrielBailey74
« Reply #6 - Posted 2013-07-10 04:33:18 »

@GabrielBailey74
I want to know what operations you exactly did to increase the performance. Can I use
GraphicsDevice.createCompatibleImage()
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
GraphicsDevice.createCompatibleImage()
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


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

@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

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!