We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Load Image at RunTime
Page Index Toggle Pages: 1
Load Image at RunTime (Read 1101 times)
Load Image at RunTime
Aug 27th, 2007, 4:02pm
 
I am writing an application and I need the ability for the user to load Images at runtime into it. Or better yet, designate a file path that will hold hundreds of images that my program will need to access. When I export to application, the data directory is gone. How do I allow a user to load their own files into my app?

Thanks!
Re: Load Image at RunTime
Reply #1 - Aug 27th, 2007, 4:52pm
 
1.
http://processinghacks.com/hacks/filechooser

2.
http://www.sojamo.de/iv/index.php?n=12

additionally a drop event is implemented in controlP5 which allows the user to drag files, folders, text, content from browsers and other applications onto a sketch. the dropped content is then accessible or can be loaded within a sketch.

Re: Load Image at RunTime
Reply #2 - Aug 27th, 2007, 5:55pm
 
Drag'n'drop works very well using the script from here:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1136283692

so, d'n'd does not need controlP5... if you only need file dropping.

Lenny, have you successfully used the filechooser hack?
I'm always having strange problems (threading?) with it... e.g. the chooser opening twice & stuff like that.
Re: Load Image at RunTime
Reply #3 - Aug 27th, 2007, 6:35pm
 
Thanks guys..Im very interested in drag and drop. When this allow the user to drag and drop a folder into the screen and then let me at a later time pull images that are in a sub folder of that directory?
Re: Load Image at RunTime
Reply #4 - Aug 27th, 2007, 7:02pm
 
jher:
no, i didn't use it myself.
(i like that d'n'd, awesome feature;)
but a friend of mine uses it for an application and he seems to be lucky with it...
Re: Load Image at RunTime
Reply #5 - Aug 27th, 2007, 11:13pm
 
jher, for sure. You should have a look at the d'n'd example... and then http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html should get you going.

For instance, there are several useful methods in the File-Class:
isDirectory()
listFiles()
getPath()
getName()
and so on.


Well... couldn't resist to try it myself Wink

Quote:


import java.awt.dnd.*;
import java.awt.datatransfer.*;

int drops = 5;

void setup() {
 size(200, 200);
 fill(255);
 noStroke();
 rectMode(CENTER);
}

void draw() {
 background(0);
 
 rect(width/2, height/2, drops, drops); // draw box
}

void loadFile(File file) {
 println("someone dropped " + file.getPath());
 
 // we check if the dropped "file" is a folder/directory
 if (file.isDirectory()) {
   println(file.getName() + " seems to be a directory... reading contents");
   
   // so, we can ask that directory for its contents... which will result in an array of File-Objects
   File[] folderContents = file.listFiles();
   println("this folder has " + folderContents.length + " files and/or folders in it:");
   
   for (int i=0; i<folderContents.length; i++) {
     println("|_ " + folderContents[i].getName());
     drops+=3;
   }
   
 } else {
   println("this is no directory. what else could it be?");
 }
 
}


// DRAG'N'DROP

DropTarget dt = new DropTarget(this, new DropTargetListener() {
 public void dragEnter(DropTargetDragEvent event) {
   // debug messages for diagnostics
   //System.out.println("dragEnter " + event);
   event.acceptDrag(DnDConstants.ACTION_COPY);
 }

 public void dragExit(DropTargetEvent event) {
   //System.out.println("dragExit " + event);
 }

 public void dragOver(DropTargetDragEvent event) {
   //System.out.println("dragOver " + event);
   event.acceptDrag(DnDConstants.ACTION_COPY);
 }

 public void dropActionChanged(DropTargetDragEvent event) {
   //System.out.println("dropActionChanged " + event);
 }

 public void drop(DropTargetDropEvent event) {
   //System.out.println("drop " + event);
   event.acceptDrop(DnDConstants.ACTION_COPY);

   Transferable transferable = event.getTransferable();
   DataFlavor flavors[] = transferable.getTransferDataFlavors();
   int successful = 0;

   for (int i = 0; i < flavors.length; i++) {
     try {
       Object stuff = transferable.getTransferData(flavors[i]);
       if (!(stuff instanceof java.util.List)) continue;
       java.util.List list = (java.util.List) stuff;

       for (int j = 0; j < list.size(); j++) {
         Object item = list.get(j);
         if (item instanceof File) {
           File file = (File) item;
   
           // passing the whole file-object here
           loadFile(file);
         }
       }

     }  
     catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
}
);


Re: Load Image at RunTime
Reply #6 - Aug 28th, 2007, 2:41pm
 
Thanks dudes....that worked like a charm!!!!
Page Index Toggle Pages: 1