user2517280
user2517280

Reputation: 221

Using .png as label icon

I created a small program which ran great, I then needed to add a GUI so ported all my code over using NetBeans to make things a bit easier. Ive got it working great when I use jpgs but obviously that isnt the best format to use. Before, when I used pngs it worked fine, now I get a big list of errors in the console which I never would get before. Essentially the program reads the file the user selects as a bufferedimage and it also sets the label in the GUI as the selected file, or it did until creating the GUI. As I say it works fine with jpg, no issues at all so im not too sure what is happening. Its also reading it as a bufferedimage fine as im printing the filepath and bufferedimage variable to the console so can see both of those are fine, then a big list of errors which are the following:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [I cannot be cast to [B at java.awt.image.ColorModel.getAlpha(ColorModel.java:833) at java.awt.image.ColorModel.getRGB(ColorModel.java:878) at sun.awt.image.ImageRepresentation.convertToRGB(ImageRepresentation.java:305) at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:564) at java.awt.image.AreaAveragingScaleFilter.accumPixels(AreaAveragingScaleFilter.java:213) at java.awt.image.AreaAveragingScaleFilter.setPixels(AreaAveragingScaleFilter.java:252) at sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource.java:102) at sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:187) at sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:66) at sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSource.java:80) at java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:183) at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:727) at sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:802) at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1018) at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186) at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3084) at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3069) at javax.swing.ImageIcon.paintIcon(ImageIcon.java:413) at sun.swing.plaf.synth.SynthIcon.paintIcon(SynthIcon.java:67) at javax.swing.plaf.synth.SynthGraphicsUtils.paintText(SynthGraphicsUtils.java:396) at javax.swing.plaf.synth.SynthLabelUI.paint(SynthLabelUI.java:213) at javax.swing.plaf.synth.SynthLabelUI.update(SynthLabelUI.java:177) at javax.swing.JComponent.paintComponent(JComponent.java:778) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paint(JComponent.java:1063) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443) at javax.swing.RepaintManager.paint(RepaintManager.java:1236) at javax.swing.JComponent._paintImmediately(JComponent.java:5169) at javax.swing.JComponent.paintImmediately(JComponent.java:4980) at javax.swing.RepaintManager$3.run(RepaintManager.java:796) at javax.swing.RepaintManager$3.run(RepaintManager.java:784) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706) at javax.swing.RepaintManager.access$1000(RepaintManager.java:62) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:688) at java.awt.EventQueue$3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:697) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


With my code for loading the icon being:

image1 = ImageIO.read(file);            
            ImageIcon imageIcon1 = new ImageIcon(image1);
            Image redInput = imageIcon1.getImage().getScaledInstance(300, 300, Image.SCALE_SMOOTH);
            imageIcon1.setImage(redInput);
            redLabel.setIcon(imageIcon1);

I only have 1 class called NewJFrame, any help would be massively appreciated!

Upvotes: 0

Views: 688

Answers (2)

Holger
Holger

Reputation: 298103

Since it doesn’t seem to be an error in your code, I suggest to try to work-around this problem as you can’t change the AWT implementation.

BufferedImage img=ImageIO.read(file);
Image redInput=new AffineTransformOp(
  AffineTransform.getScaleInstance(300.0/img.getWidth(), 300.0/img.getHeight()),
  AffineTransformOp.TYPE_BICUBIC).filter(img, null);
imageIcon1= new ImageIcon(redInput, file.toString());
redLabel.setIcon(imageIcon1);

This code goes to a different path dedicated to the specialized processing of a BufferedImage. There’s no guaranty the this doesn’t encounter the same bug (as I hadn’t have your file to test it). But I think, it’s worth a try as the change to your code is rather small.

If that doesn’t work you can try the to convert the color space manually right after loading as already suggested by others:

BufferedImage img=ImageIO.read(file);
final int w = img.getWidth(), h = img.getHeight();
BufferedImage bi=new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
bi.setRGB(0, 0, w, h, img.getRGB(0, 0, w, h, null, 0, w), 0, w);
img=bi;
// proceed as normally

Upvotes: 1

Josh M
Josh M

Reputation: 11939

It appears you are trying to cast an int[] to a byte[].

Upvotes: 0

Related Questions