JVx has a simple mechanism to create “screenshots” from components or the entire application. But, if you want to save the screenshot, you need a file/directory chooser and some application logic.
Here's a simple solution for using the system clipboard for your screenshots. This example is based on JVx's SwingUI.
public void doScreenshot() throws Throwable { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IDimension dim = getSize(); IImage image = capture(dim.getWidth(), dim.getHeight()); image.saveAs(baos, ImageType.PNG); try { TransferableImage trans = new TransferableImage(baos.toByteArray()); Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); c.setContents(trans, trans); } catch (Exception e) { //fallback to file chooser getApplication().getLauncher().saveFileHandle( new FileHandle("screenshot.png", baos.toByteArray())); } }
public class TransferableImage implements Transferable, ClipboardOwner { private static final DataFlavor[] DATAFLAVORS = new DataFlavor[] {DataFlavor.imageFlavor}; private Image image; public TransferableImage(byte[] pData) throws IOException { image = ImageIO.read(new ByteArrayInputStream(pData)); } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(DataFlavor.imageFlavor) && image != null) { return image; } else { throw new UnsupportedFlavorException(flavor); } } public DataFlavor[] getTransferDataFlavors() { return DATAFLAVORS; } public boolean isDataFlavorSupported(DataFlavor flavor) { return ArrayUtil.contains(getTransferDataFlavors(), flavor); } public void lostOwnership(Clipboard clipboard, Transferable contents) { } }
Simply paste the image in another application, e.g., a text document.