Image question

Howdy again,

I have an image that is a background, a buffered imaged to be exact. Then I put my components on top of that, like a button, a label, and other interesting things.

Then I have the DrawingPanel (you can see my other post here: http://www.java-gaming.org/forums/index.php?topic=13791.0) that handles drawing the image.

Works great. Except, the performance is not that great. Trying to render that image increases my CPU time by +20-30%. I also realize that I do not have to keep on drawing it over and over again because its the background of a window and thus, will never change. I know you can put an image in a JLabel, and that won’t cause a huge performance increase. I would do this with the background on this one but, I need to place comonents on top of the Label and that is not possible!

So, is there a way to draw an image only once and then not draw it again, or only redraw it if it happens to change, like the frame is resized?

Thanks.

well in my experience even if it’s a background image you have to draw it each time becouse with each frame you have to erase marks from the previous frame, so your drawing objects actually move. About performance loss, I don’t know if it is normal to have such performance loss (hey, you’re drawing very large image, it takes time) but make sure it’s hardware accelareted. Since it’s a background image you can use image format that dosen’t have transparancy or translucency, like jpg, don’t know how java2d handles that but it might be faster.

Man you make rumors just because such a plain&ugly background image zz

The background image is drawn whenever it needs to be drawn, not “all the time” like you call it.
This means painting the image like if it would be in a JLabel except:

  • background is invalidated because of a layout change
  • a non-opaque component is repainted.

Under normal circumstances blits (image-paints) are very fast.
It could be that your image is rendered using software loops because you request some operation which can’t be done in hardware with the pipeline you are using… I would run your application with -Dsun.java2d.trace=count enabled to see where all the time goes.

lg Clemens

hmm… I thought we’re talking about drawing image in game loop and then components on top of it… couse there’s no way drawing a image takes +30% cpu time for just a swing menu (when implemented properly, repaint() on request).