Reputation: 2845
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?
Upvotes: 3
Views: 470
Reputation: 205765
What's a more practical solution: having a thread cache all of the images somewhere accessible on application start-up or sending a thread for each individual image. I use roughly 2-3 MBs of images throughout the application.
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.
Upvotes: 3