When using Clipboard, Toolkit and Transferable, I get an error objecting to image width and height
Posted
by Mike King
on Stack Overflow
See other posts from Stack Overflow
or by Mike King
Published on 2010-06-13T11:31:35Z
Indexed on
2010/06/13
11:42 UTC
Read the original article
Hit count: 302
java
When I run the following code it triggers an error message. The error message is shown below the code.
What code changes, or changes to the image file, are needed to fix this error?
Help will be appreciated.
import java.awt.*;
import java.awt.datatransfer.*;
public class LoadToClipboard {
public static void main( String [] args ) {
Toolkit tolkit = Toolkit.getDefaultToolkit();
Clipboard clip = tolkit.getSystemClipboard();
clip.setContents( new ImageSelection( tolkit.getImage("StackOverflowLogo.png")) , null );
}
}
class ImageSelection implements Transferable {
private Image image;
public ImageSelection(Image image) {
this.image = image;
}
// Returns supported flavors
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
}
// Returns true if flavor is supported
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
// Returns image
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (!DataFlavor.imageFlavor.equals(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0 at
java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999) at
sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:1994) at
sun.awt.windows.WDataTransferer.imageToPlatformBytes(WDataTransferer.java:267) at
sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1123) at
sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163) at
sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73) at
sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93) at
automateSignature.LoadToClipboard.main(LoadToClipboard.java:8)
I have tried to find a place in the code where width and height can be specified, but have not succeeded. I also examined the properties of the jpg file and the w and h are specified.enter code here
© Stack Overflow or respective owner