保存ボタンがクリックされたときにファイル名を指定する必要があり、scribbledraganddrop のコードを取得しました。このコードの保存ボタン オプションを実装したいのですが、imageIO.write で取得する必要がある bufferedimage/buffered 値を見つけることができません。入力ストリームとして。
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());
}
}
私のコード全体はここにあります: https://github.com/mitshine/scribledraganddrop/issues/1 ここで 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);
}