I would like to know if is possible to say for example drag and drop an image file to a running Processing Sketch and show the image on it, if so can you show me an example?
1
// Basic drop event listening/handling example. // Run the sketch and drop various things on its canvas: // files/folders, images, rendered html, plain text, etc. void setup() { size(700, 400); textSize(16); textLeading(18); frame.setTitle("Drop something here..."); } void draw() {} // Called when the user drops something on the canvas. void dropped(Drop drop) { // Set the drop location as the frame title int x = drop.x; int y = drop.y; frame.setTitle( String.format("Last drop location: x=%d, y=%d", x, y)); background(50); StringBuffer info = new StringBuffer(); // Is the dropped object text? (For some non-text objects, // drop.text() returns the associated path) String plain = drop.text(); if (plain != null) info.append("FOUND PLAIN TEXT:\n") // grab a preview eg. the 200 first chars .append(str_preview(plain, 200)) .append("\n\n"); // Is it "rich text"? Eg. dropped from a rendered html or rtf window. String rich = drop.richtext(); if (rich != null) info.append("FOUND RICH TEXT:\n") .append(str_preview(rich, 200)) .append("\n\n"); // Pixel data or file(s) that could be loaded as PImage(s)? PImage[] images = drop.images(); if (images != null) { // Display the number of images successfully loaded, // use the first image as background info.append("FOUND " + images.length + " IMAGES\n\n"); image(images[0], 0, 0, width, height); fill(50, 190); // make it look semi-transparent rect(0, 0, width, height); } // File(s)/folder(s)? Eg. dropped from the OS file explorer. String[] paths = drop.paths(); if (paths != null) { info.append("FOUND PATHS/URLS:\n"); for (String i : paths) info.append(i).append('\n'); info.append('\n'); } fill(255); text(info.toString(), 20, 20, width-40, height-40); } // Strips leading whitespace, returns the n first characters of s String str_preview(String s, int n) { s = s.trim(); return s.substring(0, min(n, s.length())); } // The code below implements the drop event listening/handling // and the Drop class. For less clutter put it in a separate // pde file/tab. import java.awt.dnd.*; import java.awt.datatransfer.*; // Note: droptarget.setActive(false) to stop listening for drops, // droptarget.isActive() to know the current status (true/false). DropTarget droptarget = new DropTarget(this/*this PFrame*/, new DropTargetAdapter() { public void drop(DropTargetDropEvent event) { event.acceptDrop(DnDConstants.ACTION_COPY); DataFlavor[] flavors = event.getCurrentDataFlavors(); if (flavors.length == 0) error("unsupported data flavor/type"); else { DataFlavor f = flavors[0]; Object data = null; try { data = event.getTransferable().getTransferData(f); } catch (Exception e) { error(e.toString()); } if (data != null) dropped(new Drop(event, f, data)); } event.dropComplete(true); } void error(String what) { frame.setTitle("*A drop error occured (see console)"); System.err.println("Drop error:\n" + what); } }); class Drop { int x, y; Object data; DataFlavor flavor; DropTargetDropEvent event; Drop(DropTargetDropEvent e, DataFlavor f, Object d) { event = e; flavor = f; data = d; x = e.getLocation().x; y = e.getLocation().y; } String text() { try { return (String) event.getTransferable() .getTransferData(DataFlavor.stringFlavor); } catch (Exception e) {} return null; } String richtext() { if (flavor.getMimeType().startsWith("text")) { Scanner s = data instanceof Reader ? new Scanner((Reader)data) : data instanceof InputStream ? new Scanner((InputStream)data) : null; s.useDelimiter("^\\s"); // alien hack to preserve whitespaces if (s != null) { StringBuilder buf = new StringBuilder(256); while (s.hasNext()) buf.append(s.next()); return buf.toString(); } } return null; } File[] files() { if (flavor.equals(DataFlavor.javaFileListFlavor)) { Listlist = (List ) data; return list.toArray( new File[ list. size()]); } return null; } String[] paths() { if (data instanceof URL) return new String[]{ ((URL)data).toString() }; File[] f = files(); if (f != null) { int i = f. length; String[] paths = new String[i]; while (i-- > 0) paths[i] = f[i].getAbsolutePath(); return paths; } return null; } PImage[] images() { // Single image if (flavor. equals(DataFlavor.imageFlavor)) return new PImage[]{ new PImage((Image) data) }; // Load from paths / urls. // TODO async loading? Skip files with non-image extension? List< PImage> images = new ArrayList< PImage>(); PImage tmp = null; if (flavor. equals(DataFlavor.javaFileListFlavor)) { for (File f : (List ) data) if (f.isFile() && f.exists() && (tmp = loadImage(f.getAbsolutePath())) != null) images. add(tmp); } else { String[] paths = paths(); if (paths != null) for ( String path : paths) if ((tmp = loadImage(path)) != null) images. add(tmp); } return images. size() == 0 ? null : images.toArray( new PImage[images. size()]); } public String toString() { return data.toString(); } }