简体   繁体   English

将png图像添加到Java小程序

[英]Adding a png image to a java applet

I am very new to Java, I want to add a PNG image to my applet. 我对Java非常陌生,我想向我的applet添加一个PNG图像。 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) 线程“ Image Fetcher 0”中的异常java.lang.OutOfMemoryError:java.awt.image.Raster.createPackedRaster(未知源)处java.awt.image.DataBufferInt。(未知源)处的Java堆空间Sun.awt.image.ImageRepresentation.setPixels上的.DirectColorModel.createCompatibleWritableRaster(未知源)在sun.awt.image.ImageDecoder.setPixels上的。在sun.awt.image.ImageFetcher处的sun.awt.image.PNGImageDecoder.produceImage(未知源)处在sun.awt.image.InputStreamImageSource.doFetch(未知源)处.fetchloop(未知来源)位于sun.awt.image.ImageFetcher.run(未知来源)

When I try adding a jpeg image it works fine. 当我尝试添加jpeg图像时,效果很好。 How do I work around this? 我该如何解决?

How big is your PNG image? 您的PNG图片有多大? 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. 从给定的堆栈跟踪来看,当尝试从加载的图像创建BufferedImage时,似乎堆空间不足。 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). 您可以首先尝试增加堆大小,以查看是否可以解决问题-请参阅applet的java_arguments参数,尽管这不是永久解决方案(没人喜欢占用内存的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. OutOfMemoryError可能意味着某处内存泄漏。 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. 尝试在visualvm中使用探查器,因为它会告诉您最大的使用者。

Hope it helps :) 希望能帮助到你 :)

-- Flaviu Cipcigan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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