With that said, I'm writing a 2D sidescroller, and everything (so far) was working fine, until I tried to load my first background. The background is a large image that would scroll as the player moves. (It's an 11000 px x 6000 px .png file that is 6.3 MB)
I have several other animations/images that loaded perfectly fine before this.
The way I'm handling animations and images (if this is a bad idea please let me know so I can fix it.) is to load them all in the constructor of the game into two separate handler objects. For example, I have a DataStreamReader that reads an initial_load.txt file and passes the names, paths, and number of frames from this txt file to an AnimationHandler object as Strings. The animation handler takes these strings and uses them to load each frame into an animation object and then the animation into an ArrayList<Animation>.
for(int i=0;i<numFrames;i++){
ImageIcon img = new ImageIcon(this.getClass().getResource(path+name+i+".png")).getImage();
frames.add(new Frame(img));
}
animations.add(new Animation(frames, name));
For each object I create, I pass it the animationHandler and it finds the appropriate animations for itself. This way I'm not loading the images for each separate object. (This is how I've done it for my earlier games.)What I've learned is that Java creates a separate Thread for each image it is trying to load. After too many threads are loaded it will produce the error I'm getting:
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:230) at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:535) at apple.awt.OSXImageRepresentation.setPixels(OSXImageRepresentation.java:58) at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120) at sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:531) at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:452) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
Nothing I've read has really explained how to go about fixing this.
That's everything I know. I'm really not sure what to do although I'll keep looking.
Any help is greatly appreciated,
Thanks
Peter






