Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  BufferStrategy vs. BufferedImage  (Read 8062 times)
0 Members and 1 Guest are viewing this topic.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Posted 2011-03-12 01:13:49 »

What is the difference between double buffering by:
-creating BufferedImage, drawing in it, and drawImage() onto screen,
-using BufferStrategy.

I have only used BufferStrategy once and it doesn't seem that much different.

Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2011-03-12 02:19:36 »

BufferStrategy is way faster . I cannot quantize this "way faster" right now, but I remember a few years ago when I was developing my first java game I almost cried with hapiness when I changed double buffering with BufferedImage to BufferStrategy and gaind something like 20 FPS .

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-03-12 02:26:43 »

Hmm, ok I will look into it a bit more.

But I still don't understand how it is faster Huh

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline IronclawsBt

Junior Devvie


Medals: 1



« Reply #3 - Posted 2011-03-12 03:19:53 »

BufferStrategy has the option of pointer flipping. Instead of copying the off screen buffer to the screen's memory, the buffer simple says "Use this for memory the screen now". It is not only faster, but also reduces tearing.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-03-12 03:50:01 »

Oh so that's the difference!

Thanks!!

Offline dah01

Junior Devvie


Medals: 7



« Reply #5 - Posted 2011-03-12 05:40:11 »

This is valuable knowledge. Do you have any code samples or links with exanples of the difference in implementing this switch? Is it a class-for-class swap or is there a drastic pattern change required?
Offline IronclawsBt

Junior Devvie


Medals: 1



« Reply #6 - Posted 2011-03-12 05:46:53 »

First stop for all API questions should be the Javadoc (http://download.oracle.com/javase/6/docs/api/). Look up BufferStrategy and BufferCapabilities. The class is well documented.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-03-12 19:45:50 »

There is createBufferStrategy(int) in java.awt.Canvas and java.awt.Window. You call that first then get the BufferStrategy by calling getBufferStrategy(). All there is left is to getDrawGraphics() to get the Graphics object and then show() to show the image on the screen.

Hope that helped Grin

Offline dah01

Junior Devvie


Medals: 7



« Reply #8 - Posted 2011-03-13 03:53:12 »

It did! I think you are the flash, replying to all my threads.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-03-13 03:57:09 »

Oh wow I didn't know you were the OP of that other thread about AWT Tongue

Glad to help anyway Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #10 - Posted 2011-03-13 07:29:27 »

How does BufferStrategy differ from just using a Swing JPanel and drawing on that? JPanel automatically double buffers, doesn't it? Really simple to use. I can do redraws under 20msec with it. Huh It seems quite good for beginning simple animations, as well as move-based or puzzle-based games. The "smoke" the click&drag animation in Hexara http://www.hexara.com are all done that way.

"It's after the end of the world! Don't you know that yet?"
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #11 - Posted 2011-03-13 10:42:33 »

Generally go for bufferstrategy as buffer flipping is faster than bit copying.  That is unless you are creating the entire screen image by manually manipulating bits in CPU memory, rather than on the GPU.  In this case, you end up copying the entire buffer every frame anyway.  If you implement BufferStrategy in this case, and the hardware doesn't support it, then you can end up copying the buffer twice every frame!

Time flies like a bird. Fruit flies like a banana.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-03-13 18:51:03 »

If you want to use JFrame or JApplet, then draw on a JComponent and then you don't need to worry about double buffering.
If you want to use Frame or Applet, then draw on a Canvas and use BufferStrategy.

Offline dah01

Junior Devvie


Medals: 7



« Reply #13 - Posted 2011-03-13 22:58:18 »

So, let me get this straight: You can simply draw on the panel and it will auto double buffer? That sounds perfect.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2011-03-13 23:10:40 »

I don't recommend drawing on a JPanel, try JComponent.

Offline philfrei
« Reply #15 - Posted 2011-03-14 07:30:01 »

Yes, you can draw on a JPanel and it automatically double buffers. ra4king is adamant that JComponent is better. There are probably good reasons and I defer to him, as I'm kind of cruising on the surface level of Java in many respects and have never used JComponent. But if you are looking for simplicity, you CAN do this:

Top level, JFrame. Then, instead of trying to do anything fancy with the "content pane" of the JFrame, just add a subclassed JPanel and draw/animate your objects on that. Easy peasy. We are talking beginning, get feet wet, intro to Java game programming, nothing fancy. But one can do a fair bit with this set up.

I think the main hazard/complication is that repaint() calls have to be done from some vantage point other than the JPanel itself, due to an aspect of "passive rendering". If you put your game loop or timer on the JPanel with the animation, the Event Dispatch Thread will think it can be "more efficient" by collapsing the repaint calls together rather than treating each one as a separate event.

So, my solution has been to create a separate class with a Timer and have the Timer call the JPanel's repaint() function. I use the java.util timer, and set it to repeat calls every 30 msec or thereabouts. But you could do something similar with a game loop. Main point, the game loop or timer should not be part of the JPanel class you are animating.

Do I hear howls of disagreement?  Cool One can refactor in active rendering or other fancier animation schemes later.

"It's after the end of the world! Don't you know that yet?"
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-03-14 11:33:05 »

JPanel is used as a container. You don't draw on a container, you draw on a component Tongue

And yes, I agree that calling repaint() is passive rendering, which is why it's good for beginners, but not recommended to keep doing.

When I made a quick test, I went up to 100 FPS without the repaint() calls being collapsed together so it's not really that bad.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2011-03-14 14:21:46 »

JPanel is used as a container. You don't draw on a container, you draw on a component Tongue

1  
public class JComponent extends Container

Tongue

The reason to use JPanel over JComponent is that JPanel calls setDoubleBuffered(true) in its constructor, while JComponent does *not*.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-03-14 17:36:14 »

1  
public class JComponent extends Container

Tongue

The reason to use JPanel over JComponent is that JPanel calls setDoubleBuffered(true) in its constructor, while JComponent does *not*.
Yes yes I know Tongue but try adding components to a JComponent Grin
JComponent is automatically double buffered without any extra variables/method calls Wink

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #19 - Posted 2011-03-14 18:11:44 »

The real gotcha is that JRootPane calls setDoubleBuffered(true) in its constructor, which causes all of its child components (i.e., everything) to be double-buffered, no matter what their value of their double-buffer flags are.
Offline onlytoine

Senior Newbie





« Reply #20 - Posted 2011-03-16 16:05:38 »

Hello,

It is my first message in this forum  Shocked

The fastest way to render properly an image is to:
- use a Panel (AWT)
- use a BufferStrategy (Page Flipping)
- use compatible images

Other things to take care?  Huh

Toine
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2011-03-16 18:15:55 »

Use Canvas instead of Panel, other than that you're good Smiley

Offline onlytoine

Senior Newbie





« Reply #22 - Posted 2011-03-16 19:54:17 »

Hey  Smiley

Could you just tell me why canvas is better than panel?

Cheers,
Toine
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #23 - Posted 2011-03-16 20:04:36 »

Because Canvas has access to BufferStrategy and, as the name suggests, it was made for drawing.
Panel is only used as a container.

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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!