简体   繁体   中英

Adding a png image to a java applet

I am very new to Java, I want to add a PNG image to my applet. From some reason when I add this image I receive the following error:

Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.(Unknown Source) at java.awt.image.Raster.createPackedRaster(Unknown Source) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source) at sun.awt.image.ImageRepresentation.createBufferedImage(Unknown Source) at sun.awt.image.ImageRepresentation.setPixels(Unknown Source) at sun.awt.image.ImageDecoder.setPixels(Unknown Source) at sun.awt.image.PNGImageDecoder.sendPixels(Unknown Source) at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source) at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source) at sun.awt.image.ImageFetcher.fetchloop(Unknown Source) at sun.awt.image.ImageFetcher.run(Unknown Source)

When I try adding a jpeg image it works fine. How do I work around this?

How big is your PNG image? Judging by the stack trace you have given me, it appears that you run out of heap space when you try to create a BufferedImage from the image you load. You could try resizing the image to a smaller size.

You could first try increasing the heap size, to see if that solves the problem - see the java_arguments parameter to an applet, though that won't be a permanent solution (no-one likes a memory hogging applet). You should also run your application through a profiler and see what is consuming most of the memory. An OutOfMemoryError could mean a memory leak somewhere. It could even be outside of your image-loading class, as the heap could have already been overfilled when trying to load an image, and the image loading causing it to fill up. Try to use the profiler in the visualvm as it will tell you the biggest consumers.

Hope it helps :)

-- Flaviu Cipcigan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM