Java 需要存储缓冲值和生成的图像,并在单击“保存”按钮时存储该图像
单击save按钮时,我需要提供一个文件名,我得到了scribbledraganddrop的一些代码,我想为这段代码实现save按钮选项,但找不到应该在imageIO.write中作为inputstream获取的BuffereImage/buffered值Java 需要存储缓冲值和生成的图像,并在单击“保存”按钮时存储该图像,java,drag-and-drop,save,transfer,scribble,Java,Drag And Drop,Save,Transfer,Scribble,单击save按钮时,我需要提供一个文件名,我得到了scribbledraganddrop的一些代码,我想为这段代码实现save按钮选项,但找不到应该在imageIO.write中作为inputstream获取的BuffereImage/buffered值 JFileChooser save = new JFileChooser(); int option = save.showSaveDialog(frame); if (option == JFileChoose
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(frame);
if (option == JFileChooser.APPROVE_OPTION) {
try {
System.out.println(Arrays.toString(Scribble.supportedFlavors));
//InputStream in = new ByteArrayInputStream(Scribble.supportedFlavors); // not working??
//System.out.println(scribbles.toString());
//BufferedImage awtImage = ImageIO.read(in);
BufferedImage awtImage = new BufferedImage(frame.getWidth(),frame.getHeight(),BufferedImage.TYPE_INT_RGB);
File selected = save.getSelectedFile();
//System.out.println(in);
frame.paint(awtImage.getGraphics());
try {
ImageIO.write(awtImage, "png", selected);
System.out.println("panel saved as image at: " + selected.getPath());
} catch (Exception evt) {
System.out.println("panel not saved" + evt.getMessage());
}
} catch (Exception ex) { // again, catch any exceptions and...
System.out.println(ex.getMessage());
}
}
我的全部代码可以在这里找到:
此处Transferhandle的实现方式如下:
//====== The following methods implement the Transferable interface =====
// This is the custom DataFlavor for Scribble objects
public static DataFlavor scribbleDataFlavor = new DataFlavor(
Scribble.class, "Scribble");
// This is a list of the flavors we know how to work with
public static DataFlavor[] supportedFlavors = { scribbleDataFlavor,
DataFlavor.stringFlavor };
/** Return the data formats or "flavors" we know how to transfer */
public DataFlavor[] getTransferDataFlavors() {
return (DataFlavor[]) supportedFlavors.clone();
}
/** Check whether we support a given flavor */
public boolean isDataFlavorSupported(DataFlavor flavor) {
return (flavor.equals(scribbleDataFlavor) || flavor
.equals(DataFlavor.stringFlavor));
}
/**
* Return the scribble data in the requested format, or throw an exception
* if we don't support the requested format
*/
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (flavor.equals(scribbleDataFlavor)) {
return this;
} else if (flavor.equals(DataFlavor.stringFlavor)) {
return toString();
} else
throw new UnsupportedFlavorException(flavor);
}