Slow background image in a JPanel
I have a simple JPanel class which only draw the background image. I'm using the StackLayout of other JPanels ontop of the background panel, doing different stuff like draw rectangles. But everything is in slow motion. But if I comment out the background panel, everything runs smoothly. The background image is about 1100x1500 pixels (600kb png).
What am I doing wrong? Is there a way to optimize it?
I have searched the forum trying to find a solution, I tried using GraphicsEnvironment etc., but don't seem to help.
publicclass Backgroundextends JPanel{
private BufferedImage backgroundImage;
private ResourceBundle bundle;
public Background(){
bundle = Application.getResourceBundle();
try{
backgroundImage = ImageIO.read(getClass().getResource(bundle.getString("background")));
}
catch (IOException e){
e.printStackTrace();
}
setPreferredSize(new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight()));
}
@Override
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0,this);
}
}
[1866 byte] By [
williwa] at [2007-11-27 4:44:10]

# 3
> The background image is about 1100x1500 pixels (600kb png).
Just a rectification :
1100x1500 pixels image is 600kb on disk, but png is a (lossless) compression format. Once in memory, your image fits :
- 4 950 000 Bytes if it's 24bits colors (no tranparency)
- 6 600 000 Bytes if it 's 32bits colors (BufferedImage.TYPE_INT_ARGB)
# 5
1100x9000 !!!
This not a background image ! (let me know.... Is your background displayed on 10 screens ?)
1) cut your files with a picture modifier tool (Gimp, Photoshop,...) if you have enough memory on your computer
2) If the resulting images fit in your memory, just increase the Java heap size (-Xmx option)
java -Xmx512m my.package.MyClass
will launch my.package.MyClass in a JVM that may allocate a maximum of 512Mo into memory.
1100x9000 = 9900000 pixels
with 32bits pixels (default TYPE_INT_ARGB) : 9900000x4 = 39 600 000 (about 39Mo only for 1 image!!!)
Default max java heap size is 32Mo I think. So you stop with OutOffMemoryError while loading it.
# 6
Yea, it's quite big. It's a "background" as in I show the image in the background of a layeredpane and do stuff like draw etc on the layers on top of it. The layeredpane is in a scrollpane, so the user can scroll.
I can't cut the image as it's the user that put the image in.
How can I increase the heap size for a jar file?
Is it better to use VolatileImage?