private static BufferedImage toCompatibleImage(BufferedImage image)
{
// obtain the current system graphical settings
GraphicsConfiguration gfx_config = DisplayManager.getGraphicsConfiguration();
/*
* if image is already compatible and optimized for current system
* settings, simply return it
*/
if (image.getColorModel().equals(gfx_config.getColorModel()))
{
image.setAccelerationPriority(1.0f);
return image;
}
// image is not optimized, so create a new image that is
BufferedImage new_image = gfx_config.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
// get the graphics context of the new image to draw the old image on
Graphics2D g2d = (Graphics2D) new_image.getGraphics();
// actually draw the image and dispose of context no longer needed
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
new_image.setAccelerationPriority(1.0f);
// return the new optimized image
return new_image;
}
Note: DisplayManager.getGraphicsConfiguration(); is from my engine, you have to get your GraphicsConfiguration however else
So I have used this in the past; when loading an image from ImageIO I would pass it through this method.
I removed it and wrote this comment back then: “// Eats double RAM with no CPU performance improvement @ drawing whatsoever.”
Also takes longer to load obviously.
I didn’t test it thoroughly; but it didn’t show any improvement, so I haven’t used it ever in Java2D