我想从图像创建一个 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