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;
}
}
}