views:

66

answers:

3

I want to create a BufferedImage from a image but it not run. what is this exception?

code:

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:

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

The package contrib.ch.randelshofer.quaqua.util.Images in the classpath but if delete the this part it has the another exception:

boolean hasAlpha = contrib.ch.randelshofer.quaqua.util.Images.hasAlpha(image); 

change with:

boolean hasAlpha = hasAlpha(image);

it not compile and it show this message:

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
A: 

The Images class is not on your classpath.

hvgotcodes
+2  A: 

You are trying to use a third party utility class contrib.ch.randelshofer.quaqua.util.Images, but that is not found. Make sure the library providing that class is present in your classpath.

Regarding your edit: no, the lib was not properly in your classpath as you got the NoClassDefFoundError. You cannot solve that problem by simply removing the fully qualified class name. You have to both indicate in what class the hasAlpha() method can be found and make that class available in the classpath.

Jonik
A: 

Hate to say it, but Images is not in your classpath. It may be at compile time, but the class loader isn't finding it at runtime. Check that it is in your Eclipse run config, launch script, command line argument,... Can't help more without knowing how you're launching it.

Bryan Hengels
plz show me the best way to create new calsspath.
doe23