使用.png作为标签图标

时间:2013-10-28 04:10:55

标签: java swing image-processing imaging

我创建了一个运行良好的小程序,然后我需要添加一个GUI,以便使用NetBeans移植我的所有代码以使事情变得更容易。当我使用jpgs时,我的工作效果很好,但显然这不是最好的格式。之前,当我使用pngs它工作正常时,现在我在控制台中得到了一个很大的错误列表,这是我以前从未得到的。 本质上,程序读取用户选择的文件作为bufferedimage,它还将GUI中的标签设置为所选文件,或者直到创建GUI。正如我所说,它与jpg工作正常,没有任何问题,所以我不太确定发生了什么。它也将它读作bufferedimage,因为我将文件路径和bufferedimage变量打印到控制台,所以可以看到这两个都很好,然后是一个很大的错误列表,如下所示:

  

线程中的异常" AWT-EventQueue-0" java.lang.ClassCastException:   [我不能被投到[B at   java.awt.image.ColorModel.getAlpha(ColorModel.java:833)at at   java.awt.image.ColorModel.getRGB(ColorModel.java:878)at at   sun.awt.image.ImageRepresentation.convertToRGB(ImageRepresentation.java:305)     在   sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:564)     在   java.awt.image.AreaAveragingScaleFilter.accumPixels(AreaAveragingScaleFilter.java:213)     在   java.awt.image.AreaAveragingScaleFilter.setPixels(AreaAveragingScaleFilter.java:252)     在   sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource.java:102)     在   sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:187)     在   sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:66)     在   sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSource.java:80)     在   java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:183)     在   sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:727)     在   sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:802)     at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1018)at at   sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186)at at   sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3084)at at   sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3069)at at   javax.swing.ImageIcon.paintIcon(ImageIcon.java:413)at   sun.swing.plaf.synth.SynthIcon.paintIcon(SynthIcon.java:67)at at   javax.swing.plaf.synth.SynthGraphicsUtils.paintText(SynthGraphicsUtils.java:396)     在javax.swing.plaf.synth.SynthLabelUI.paint(SynthLabelUI.java:213)     在javax.swing.plaf.synth.SynthLabelUI.update(SynthLabelUI.java:177)     在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)     在   javax.swing.RepaintManager中的$ PaintManager.paint(RepaintManager.java:1443)     在javax.swing.RepaintManager.paint(RepaintManager.java:1236)at   javax.swing.JComponent._paintImmediately(JComponent.java:5169)at 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)     在   javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)     在   javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)     在   javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)     在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)     在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)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:697)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


使用我的代码加载图标:

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);

我只有一个名为NewJFrame的课程,任何帮助都会受到大力赞赏!

2 个答案:

答案 0 :(得分:1)

由于代码中似乎没有错误,我建议尝试解决此问题,因为您无法更改AWT实现。

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);

此代码转到专用于BufferedImage的专门处理的不同路径。没有保证不会遇到同样的错误(因为我没有你的文件来测试它)。但我认为,值得一试,因为对代码的更改相当小。

如果这不起作用,您可以尝试在加载后立即手动转换颜色空间,如其他人已经建议的那样:

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

答案 1 :(得分:0)

您似乎正在尝试将int[]投射到byte[]