koldewb
koldewb

Reputation: 462

Java PNG Exception

In the java application I'm working in I'm using a couple of PNG files for icons in the GUI. This works, there is no functional issue, but when booting to application is starts giving Exceptions. The amount of exceptions varies between 0 and 50 (all the same), I can't find the issue, neither a pattern.

The code I'm using for the images:

static URL imageIcon = DebugPlot.class.getResource("/myIcon.png");   
static ImageIcon icon = new ImageIcon(imageIcon);

The exception:

Uncaught error fetching image:
java.lang.ClassCastException: [I cannot be cast to [B
at java.awt.image.ColorModel.getAlpha(ColorModel.java:838)
at java.awt.image.ColorModel.getRGB(ColorModel.java:883)
at sun.awt.image.ImageRepresentation.convertToRGB(ImageRepresentation.java:305)
at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:569)
at java.awt.image.AreaAveragingScaleFilter.accumPixels(AreaAveragingScaleFilter.java:213)
at java.awt.image.AreaAveragingScaleFilter.setPixels(AreaAveragingScaleFilter.java:252)
at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:126)
at sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:558)
at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:478)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

I have googled around but I couldn't find a solution, is anyone known with this issue or how should I load the images to prevent these exceptions from happening?

Upvotes: 2

Views: 859

Answers (2)

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

Have you tried with BufferedImage and ImageIO:

BufferedImage img = null;
try {
    // Here set the path to your image
    img = ImageIO.read(new File("myIcon.png"));
} catch (IOException e) {}

ImageIcon icon = new ImageIcon(img);

Upvotes: 1

wero
wero

Reputation: 32990

This seems to be a known bug since 2003 but still unresolved:

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4937376

Upvotes: 3

Related Questions