// get required Java libraries import java.awt.*; import java.awt.image.BufferedImage; import java.awt.datatransfer.*; import java.io.IOException; public void setup() { size(500, 500); } public void draw() { if (mousePressed) { ellipse(mouseX, mouseY, 20, 20); } } public void keyReleased() { // CTRL+C: Copy to Clipboard if (key == 3) { BufferedImage clipBoardImage = new BufferedImage(width, height, (g.format == ARGB) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB); g.loadPixels(); clipBoardImage.setRGB(0, 0, width, height, g.pixels, 0, width); copyImageToClipboard(clipBoardImage); } // CTRL+V: Paste Clipboard if (key == 22) { PImage img = getClipboardImage(this); if (img == null) { return; // not an image on clipboard } int w = img.width; int h = img.height; int dx = (width - w) / 2; int dy = (height - h) / 2; image(img, dx, dy); } } /** * Clipboard copy and paste: references * http://www.devx.com/Java/Article/22326/0/page/1 * http://processing.org/hacks/hacks:clipboard ----- this link is now dead * * make everything here static so it could be implemented in a separate class * * Author: Martin Humby 2009 * */ public static void copyImageToClipboard(Image image) { TransferableImage imageSelection = new TransferableImage(image); Toolkit toolkit = Toolkit.getDefaultToolkit(); toolkit.getSystemClipboard().setContents(imageSelection, null); } public static PImage getClipboardImage(PApplet p) { Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { BufferedImage bimg = (BufferedImage) t.getTransferData(DataFlavor.imageFlavor); int w = bimg.getWidth(); int h = bimg.getHeight(); // make it ARGB to be on the safe side - makes no difference unless // clipboard image has some tranparency maybe? PImage pImg = p.createImage(w, h, PApplet.ARGB); pImg.loadPixels(); bimg.getRGB(0, 0, w, h, pImg.pixels, 0, w); pImg.updatePixels(); return pImg; } } catch (ClassCastException e) { println(e); } catch (UnsupportedFlavorException e) { println(e); } catch (IOException e) { println(e); } return null; } static class TransferableImage implements Transferable { private Image image; public TransferableImage(Image image) { this.image = image; } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (!flavor.equals(DataFlavor.imageFlavor)) { throw new UnsupportedFlavorException(flavor); } return image; } public boolean isDataFlavorSupported(DataFlavor flavor) { return flavor.equals(DataFlavor.imageFlavor); } public DataFlavor[] getTransferDataFlavors() { // in some cases there may be more than one flavour? return new DataFlavor[]{DataFlavor.imageFlavor}; } }