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 & HelpSyntax Questions › Importing a folder
Page Index Toggle Pages: 1
Importing a folder (Read 415 times)
Importing a folder
Apr 3rd, 2008, 7:21pm
 
I was looking at the Base class. I'm not sure what it's used for or how it is used. By my guess it's not for creating applets but rather development of the Processing enviroment. So, my question is either, how does the Base class work (if it used to import folders into an applet) or how would I go about importing a folder of images into an array?
Re: Importing a folder
Reply #1 - Apr 8th, 2008, 12:29pm
 
I've done this for a project of mine, sorry the examples not very well commented (if at all) and is kinda specific and a bit convoluted, it should give you an idea though.

Quote:

import java.io.File;
class Library{
 Panel p;
 File Dir;
 File[] images,videos,patches;
 File[] imgTn,vidTn;
 File[] curList;
 PImage[] curTns;
 String mode;
 LibEntry[] items = new LibEntry[16];
 
 Library(Panel pa){
   p=new Panel(pa,115,265,470,125);
   //we need to gather files from the Library Directory
   //find the directory first.
   Dir = new File(dataPath(""));
   Dir=new File(Dir.getParent()+"\\Library");
   images = getFiles("images");
   imgTn = getThumbFiles(images);
   curList=imgTn;
   curTns = getThumbs();
   int tnNo=0;
   for(int ry=0;ry<2;ry++){
     for(int rx=0;rx<7;rx++){
       items[tnNo] = new LibEntry(p.sX+(rx*60)+5,p.sY+(ry*60)+5,"image");
       if(tnNo<curTns.length){
         items[tnNo].setImage(curTns[tnNo]);
         items[tnNo].setFile(images[tnNo]);
       }
       tnNo++;
     }
   }
   
 }
 
 void update(){
   p.update();
   for(int tn=0;tn<14;tn++){
     items[tn].update();
   }
 }
 
 PImage[] getThumbs(){
   PImage[] tns =new PImage[0];
   PImage myImg;
   for(int t=0;t<curList.length;t++){
     if(curList[t].exists()){
       myImg = loadImage(curList[t].getPath());
     }else{
       myImg = nothumb;
     }
     tns=(PImage[]) append(tns,myImg);

   }
   return tns;
 }
 
 File[] getThumbFiles(File[] theList){
   File[] holder = new File[0];
   String myPath = theList[0].getParent();
   for(int t=0;t<theList.length;t++){
     if(theList[t].isFile()){
       String chk = theList[t].getName();
       chk = "tn_"+chk;
       chk = chk.substring(0,chk.length()-3);
       chk = chk+"png";
       File myTn = new File(new String(theList[t].getParent()+"\\"+chk));
       
       holder = (File[]) append(holder,myTn);
     }
     
   }
   return holder;
 }
 
 File[] getFiles(String direct){
   File retF= new File(Dir.getAbsolutePath()+"\\"+direct);
   File[] retFL = retF.listFiles();
   File[] myFL = new File[0];
   for(int t=0;t<retFL.length;t++){
     File chkr = retFL[t];
     //remove hidden files
     if(chkr.isHidden()==false){
       //remove tns from the list
       String chk = chkr.getName();
       boolean well = chk.startsWith("tn_");
       if(!well){
         myFL = (File[]) append(myFL,chkr);
       }
       
     }
     //myFL = (File[]) append(myFL,chkr);
   }
   return myFL;
 }
}

class LibEntry{
 int x,y;
 PImage thumb;
 String type,txt;
 boolean active,pressed;
 
 File file;
 LibEntry(int ix,int iy,String ty){
   x=ix;y=iy;
   type=ty;
 }
 void setImage(PImage img){
   thumb=img;
   active=true;
 }
 void setFile(File f){
   file = f;
   txt = file.getName();
 }
 void update(){
   if(pressed){
     //time to add a block to the stack (just doing image for the time being
     stackHandler grpStack = GUI.main.itemStack.control;
     //check to see if the main stack has any entries
     stackHandler mainS = GUI.main.stack.control;
     if(mainS.stackItems.size()<1){
       //it hasn't, so add it
       mainS.addGroup();
       groupStack curG = (groupStack) mainS.stackItems.get(0);
       grpStack.stackItems = curG.stackList;
     }
     
     imageStack newBlk = new imageStack(grpStack,grpStack.stackItems.size(),file);
     newBlk.me=grpStack.stackItems.size();
     grpStack.addBlock(newBlk);
   }
   
   stroke(panelEdge);
   rect(x,y,55,55);
   if(active){
     if(overRect(x,y,55,55)){
       GUI.tip.show(txt,x+40,y+0);
       stroke(bOver);
       if(mLeft){
         if(!pressed){
           stroke(select);
           pressed=true;
         }
       }
       rect(x,y,55,55);
     }
     image(thumb,x+2,y+2,50,50);
   }
   if(!mLeft){
     pressed=false;
   }
 }
 
 boolean overRect(int x, int y, int width, int height)
 {
   if (mouseX >= x && mouseX <= x+width &&
     mouseY >= y && mouseY <= y+height) {
     return true;
   }
   else {
     return false;
   }
 }
 
}



Re: Importing a folder
Reply #2 - Apr 8th, 2008, 4:33pm
 
Just looking at the first pass, I notice the java library. Processing can use java's libraries?
Re: Importing a folder
Reply #3 - Apr 8th, 2008, 4:51pm
 
Processing is (near enough) Java, and gets munged into Java behind the scenes, so you can use any Java library out there.
Page Index Toggle Pages: 1