我想从图像创建一个BufferedImage,但它不能运行。 什么是这个例外?
代码:
BufferedImage src = toBufferedImage(image1);
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see Determining If an Image Has Transparent Pixels
boolean hasAlpha = contrib.ch.randelshofer.quaqua.util.Images.hasAlpha(image);
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
例外:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: contrib/ch/randelshofer/quaqua/util/Images
at LoadImageAndScale.toBufferedImage(LoadImageAndScale.java:87)
at LoadImageAndScale.paint(LoadImageAndScale.java:59)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.ClassNotFoundException: contrib.ch.randelshofer.quaqua.util.Images
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:210)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:143)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 14 more
类路径中的包contrib.ch.randelshofer.quaqua.util.Images但如果删除此部分则有另一个例外:
boolean hasAlpha = contrib.ch.randelshofer.quaqua.util.Images.hasAlpha(image);
改变:
boolean hasAlpha = hasAlpha(image);
它没有编译,它显示此消息:
E:\3nd stage\java\test\LoadImageAndScale.java:87: cannot find symbol
symbol : method hasAlpha(java.awt.Image)
location: class LoadImageAndScale
boolean hasAlpha = hasAlpha(image);
^
1 error
答案 0 :(得分:2)
您正在尝试使用第三方实用程序类contrib.ch.randelshofer.quaqua.util.Images
,但未找到。确保提供该类的库存在in your classpath。
关于你的编辑: no ,当你得到NoClassDefFoundError时,lib在你的类路径中没有正确。只需删除完全限定的类名,就无法解决该问题。你必须在哪个类中指明可以找到hasAlpha()方法并且在类路径中使该类可用。
答案 1 :(得分:0)
Images类不在您的类路径中。
答案 2 :(得分:0)
讨厌说出来,但图像不在你的类路径中。它可能在编译时,但类加载器在运行时没有找到它。检查它是否在您的Eclipse运行配置,启动脚本,命令行参数中......如果不知道如何启动它,就无法提供帮助。