BufferStrategy and Swing Components

II’m working on a basic JFrame extension that uses active rendering via BufferStrategy. I have overridden the RepaintManager with a null version and setIgnoreRepaint(true) for all Swing components. Rather than painting directly on the JFrame, I want to paint onto a JPanel that I am placing inside the frame, so that I can take advantage of the LayoutManagers. I’ve got the basic framework up and running, but the performance seems awful. I’ve placed a JPanel inside the frame, and a JLabel inside the panel that I am displaying the fps on. When the window is maximized (1280x1024), the fastest framerate I get is around 140. This seems ridiculous for such a simple setup, since I can play Quake3 at over 200fps on this machine (yes, I know Q3 uses OpenGL, but still…).

The two factors that seem to change performance the most are…

  1. The number of buffers in the BufferStrategy. Basicly, the more buffers I use, the slower the framerate. double buffering gives 140fps, while single buffering gives 170fps. Triple buffering runs at 105fps.

  2. Use of getLayeredPane().paintComponents(g) vs paintComponents(g). Using the layeredPane’s paintComponent method is about 30fps faster than using the JFrame paintComponent method, but it causes the layout to be shifted up and to the left, which defeats the entire purpose of the layout managers.

Any suggestions would be greatly appreciated.

a Canvas object can use a BufferStrategy already. it can also be laid out using layout managers.
isn’t that what you are trying to do?

no, he wants to have swing-containers in his buffer-strategy.

To be honest I don’t know exactly the problem, maybe the painting-routines are slow not java2d.
Could you please start your app with -Dsun.java2d.trace=count and post the output here?

Thank you in advance, lg Clemens

just re-read your post and 140fps is quite VERY GOOD!
Swing was never designed to be painted over and over, keep this in mind.

You could try to create an half-transparent image which only contains your components and render this over-and-over…

lg Clemens

i’ve actually got this to work before… if you mean using bufferstrategy on a JFrame but still showng the components that are on the ContentPane…

i think it involved something like overriding my own JPanel which didn’t draw itself but did draw its children or something, and set it as the content pane…

the performance was pretty poor… Swing was not meant to be used this way, i still think Canvas is your best bet to have a component laid out and use double buffering… the other option is to override JPanel and roll your own double buffering… 140 fps is decent enough though.

I’m doing something similar with http://eengine.emedia-solutions-wolf.de. In the screenshots section you can see how a panel containing a button and a text inputbox are rendered on top of a alpha blended image inside a jframe. The fps are a bit low, but it depends on the java2d render pipeline (with the screenshot opengl was choosen and d3d gives something around 120 fps). The tutorial code is a bit outdated but latest repository code could be a good example if you are interested.

I’m creating a bufferstrategy and then render the swing components onto that graphics object with the paintAll(g) method. The drawback with this is that one have to attach the components to the jframe (in normal procedure) but call the render method explicit.

BTW.: Is it possible to have completly transparent components (swing/lightweight) without the anoying lightgrey background?

My objective was to use java layout managers to handle the placement of components within my game, so that everything stays positioned correctly if the user decides to resize the JFrame. I wanted a layout managers to handle placement of the game within the frame, and also components within the game itself (menus, etc.) I see now that doing the layout tasks manually will lead to better performance.

it’ll most likely be easier to get the layout resizing to actually work the way you want by doing it manually in the end too.

seems as BufferStrategy is aimed to be used with awt (native-heavy weight) components. for instance there seems to be no way to get a BufferStrategy for a JPanel. you can get one for a JFrame but besides it is a swing component, it has a native peer too

i’m doing something similar to yours with layout managers but i dont use a BufferStrategy. i simply depend on swing’s paint routines in ‘regular way’

using a BufferStategy and then blitting other components on top of it seems as a good idea. but be prepared for ‘weird symptoms’ especially when your gui gets complicated since you are hacking swing in some way and seems as swing doesnt like it :wink:

for your shifted layout, make sure you make that paintComponent call in awt-thread. this maybe makes no sense at first glance but i’ve seen many strange things that are fixed by simply making the call in awt-thread

you may also want to have a look at the topic http://192.18.37.44/forums/index.php?topic=10242.0

good luck
r a f t

you must put something behind that transparent component or what will it show ?
or if you mean, you want to see your desktop through that transparent components, you can manually (i dont know how or if possible ?) grap the desktop image that lies behind your frame and put it behind your components

r a f t