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]
# 1
Ok, I have now put all the layers in a JLayeredPane.I've found out that it's because every time I draw on the other layers, the background layer does a repaint. I tried to set setIgnoreRepaint(true), but it doesn't work.
williwa at 2007-7-12 9:56:04 > top of Java-index,Security,Cryptography...
# 2
It should work ok.Your background image is not very big.If you want further help post a Short, Self Contained, Compilable and Executable, Example Program ([url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url]) that demonstrates the problem.
Rodney_McKaya at 2007-7-12 9:56:04 > top of Java-index,Security,Cryptography...
# 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)

Kilwcha at 2007-7-12 9:56:05 > top of Java-index,Security,Cryptography...
# 4
Now it turns out that some of the background images can be quite large (1100x9000)This actually crashes the program. Even though I can crop the image to 1100x1500, all the stuff I'm doing on top of the image is all very slow.So I still need som suggestions. Thanks.
williwa at 2007-7-12 9:56:05 > top of Java-index,Security,Cryptography...
# 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.

Kilwcha at 2007-7-12 9:56:05 > top of Java-index,Security,Cryptography...
# 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?

williwa at 2007-7-12 9:56:05 > top of Java-index,Security,Cryptography...
# 7
Ok, it turns out that it's ImageIO that causing the problem. http://twitch.nervestaple.com/wordpress/2005/10/12/imageio-slow-to-manipulate/
williwa at 2007-7-12 9:56:05 > top of Java-index,Security,Cryptography...