Hello! As the title says, BufferedImages take up the majority of the RAM in my Java game. I use mostly pngs; some of which are transparent.
I initiate the BufferedImage objects simply like so:
BufferedImage example = ImageIO.read(Runner.class.getResource("/exampleImage.png"));
Then, I use a getCompatibleImage() function to make it the best image for rendering on the system.
static BufferedImage getCompatibleImage(BufferedImage current) {
GraphicsConfiguration gfxConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
if(current.getColorModel().equals(gfxConfig.getColorModel()))
return current;
BufferedImage optimized = gfxConfig.createCompatibleImage(current.getWidth(), current.getHeight(), current.getTransparency());
Graphics2D g2d = optimized.createGraphics();
g2d.drawImage(current, 0, 0, null);
optimized.setAccelerationPriority(1);
return optimized;
}
I use about 1219 BufferedImage objects, which is quite a lot. I read all of them at the beginning with a loading screen splash image and keep all of them as static b/c I do not want to reload them on a restart.
Please let me know what I can do to use less RAM. Thanks!