I'm writing a 2D game in Java and I'm having trouble loading images into an image handler. First off, I want to say that the system works until I start to load more than 15 images.
I have an AnimationHandler class that is given the names and paths of images and is supposed to load them into Animation classes and store those animations. I'm doing this so that each instance of an object in my game can use the same animations rather than loading them all separately.
Anyways, my problem is that when I try and load a lot of images (I'll explain why I'm going to have so many in a sec.) I get the following error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
I'm loading in the images with:
BufferedImage im = ImageIO.read(getClass().getResources(imgName));
The reason I'm going to have to load so many images is because a) one image per frame or at least one larger image per animation. b) some animations (eg. backgrounds) are huge so I have each frame of these animations split into parts.
From what I looked at, the only response anyone seems to get is to increase the amount of memory available to the JVM, but I'm not building out to .jar files yet, and I'm executing from NetBeans and don't know how to change it from inside NetBeans.
Also, the way it's loading images is to create a new thread for each image that's being loaded. I don't know if this would help but is there a way to keep track of how many images are being loaded currently, and based on that number choose to wait to load the next image?
Any help would be really appreciated, Thanks Peter
Edit: Heres the full stack trace:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:59)
at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397)
at java.awt.image.Raster.createWritableRaster(Raster.java:938)
at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1056)
at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1263)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1560)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1374)
at Loaders.ImageLoader.loadImage(ImageLoader.java:25)
at MediaHandlers.AnimationHandler.loadAnimation(AnimationHandler.java:53)
at MediaHandlers.AnimationHandler.initAnimations(AnimationHandler.java:38)
at MediaHandlers.AnimationHandler.<init>(AnimationHandler.java:22)
at Main.PlatformGame.<init>(PlatformGame.java:90)
at Main.PlatformGame.main(PlatformGame.java:105)