Jython Swing:随机发生的 Java ImageIO IOException
我使用 eclipse 和 pydev 构建了一个 Jython swing 应用程序。我使用了简单的图像作为所有按钮的背景。 95% 的时间里,一切都运转良好。然而,在从屏幕上添加或删除内容时,大约有 5% 的情况会发生这种情况:
Exception in thread "AWT-EventQueue-0" at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1310)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579)
at javax.imageio.ImageIO.read(ImageIO.java:1438)
at javax.imageio.ImageIO.read(ImageIO.java:1342)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
javax.imageio.IIOException: javax.imageio.IIOException: Error reading PNG image data
令人沮丧的是,它不容易重现,并且可能发生在我绘制 PNG 的应用程序中的任何位置。我当然可以捕获异常并让它重新导入图像,但我希望找出根本原因。
我这样称呼资源:
notPressed = ImageIO.read(pkg_resources.resource_stream('images', "button-blue.png")).getScaledInstance(width,height, Image.SCALE_SMOOTH )
我唯一的猜测是有时 pkg_resources 可能无法打开文件流,从而导致异常。关于如何追踪这个问题有什么建议吗,或者我应该庆幸我可以让它安静地失败吗?
I've built a Jython swing app using eclipse and pydev. I've used simple images for the backgrounds of all of the buttons. 95% of the time, everything works wonderfully. However, about 5% of the time when adding or removing content from the screen, this occurs:
Exception in thread "AWT-EventQueue-0" at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1310)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579)
at javax.imageio.ImageIO.read(ImageIO.java:1438)
at javax.imageio.ImageIO.read(ImageIO.java:1342)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
javax.imageio.IIOException: javax.imageio.IIOException: Error reading PNG image data
The frustrating thing is that it's not readily reproducable, and can happen anywhere in the application that I draw a PNG. I can certainly catch the exception and have it reimport the image, but I'm hoping to figure out the root cause.
I call the resources like so:
notPressed = ImageIO.read(pkg_resources.resource_stream('images', "button-blue.png")).getScaledInstance(width,height, Image.SCALE_SMOOTH )
My only guess is that sometimes pkg_resources might fail to open a filestream, causing the exception. Any suggestions on how to track this down, or should I just be happy that I can make it fail quietly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很大程度上取决于预期的延迟和图像大小。这个简单的示例将图像缓存为
List;新图像的轻微延迟是难以察觉的,因为图像是分发 JAR 的一部分。相比之下,这个示例明显滞后,因为应用程序在启动时从网络获取图像。
对于长延迟或大量大图像,您可能需要考虑
SwingWorker
,如这篇 Oracle 文章中讨论的如何使用 SwingWorker 提高应用程序性能。Much depends on the anticipated latency and image size. This simple example caches images as acuired in a
List<ImageIcon>
; the slight delay for a new image is imperceptible, as the images are part of the distribution JAR. In contrast, this example lags palpably as the application acquires images from the network at startup.For long latency or a large number of large images, you may want to consider
SwingWorker
, as discussed in this Oracle article on how to Improve Application Performance With SwingWorker.