Hello.
So far I’ve been reading images through ImageIO.read(URL). Now when my game started to have more images I noticed low fps and that they aren’t accelerated. I’m using Windows XP and I set sun.java2d.translaccel=true property but my image still dosen’t get accelarated. I know that I can only accelarate images that aren’t being modifyied (rotated, scaled…), only translated. To see if image got accelarated I use .getCapabilities(null).isAccelerated().
I’ve googled and read java2d faq but none was helpfull, I can use BufferImage constructor or .createCompatabileImage() to create empty accelarated image but when I put raster from loaded image into it, it dosen’t get accelarated. I know solution is something trivial but I’m tired of constant reading and searching. So how do I load images and make them accelarated? Thank you.
It may be that you need to drawImage() your original image onto a newly created, compatible BufferedImage.
Simon
Creating a “compatible” image is just something you have to do so that the JVM knows that the image should be a managed image. Here’s my method for doing it:
/**Creates a blank BufferedImage.
* @param width the width of the Image
* @param height the height of the Image
* @param shouldTransparencyBeAllowed whether to allow transparency in the BufferedImage
created. In most implementations of Java, allowing transparency is slower.
* @return the BufferedImage created
*/
public static BufferedImage createImage(final int width, final int height,
final boolean shouldTransparencyBeAllowed)
{
//get the GraphicsConfiguration
GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
//this enables/disables transparency and makes the Image more likely to be managed
return graphicsConfiguration.createCompatibleImage(width, height,
shouldTransparencyBeAllowed ? Transparency.BITMASK : Transparency.OPAQUE);
} //end createImage
Then you just need to draw the image you loaded to the image returned by the createImage method.
As you can see, it’s not anything difficult, and it’s something that doesn’t seem like it should have to be done at all. I don’t know why images aren’t “compatible” by default, but that’s just the way it is. It’s a small price to pay for not having to write your own graphics library.
Thank you both, this worked, I’ll put it into my loadImage method so it’s automatic
temp_bfimg = loadImage("images/teren_gol.png");
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
teren_gol_bfimg = gc.createCompatibleImage(temp_bfimg.getWidth(), temp_bfimg.getHeight(), BufferedImage.TRANSLUCENT);
teren_gol_bfimg.getGraphics().drawImage(temp_bfimg, 0, 0, null);
OK this is how it all looks like now (btw. I liked fletcher’s javadoc so from now on I’ll use it also, you can remove author part
)
/** load image from PATH and make it compatible so it can be accalarated
* @author Kova (kova1337@gmail.com)
* @param path - relative path to image, ex.: "images/some_image.png"
* @param image_type - type of BufferedImage
* @return compatible loaded BufferedImage or null if reading fails
* @see Transparency.OPAQUE, .BITMASK, .TRANSLUCENT
*/
public BufferedImage loadAndCreateCompatibleImage(String path, int image_type) {
URL url = null;
try {
url = getClass().getClassLoader().getResource(path);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage temp1_bfimg = ImageIO.read(url);
BufferedImage temp2_bfimg = gc.createCompatibleImage(temp1_bfimg.getWidth(), temp1_bfimg.getHeight(), image_type);
temp2_bfimg.getGraphics().drawImage(temp1_bfimg, 0, 0, null);
return temp2_bfimg;
} catch (Exception e) {
System.out.println("Error loading image: " + path + " " + url);
System.exit(0);
return null;
}
}