Tuesday 17 March 2009

Listing Files in the data Directories

The first task was to think about how the reader would load graphic files into the application. I could have had all files in the data directory but I decided it would be better to have a file for each data type. I never use tiff files so I decided to make directories called: GIF/JPG and PNG. I copied some images of the various types into their appropriate folder.
When the application starts the user is given the choice of which type of file they are going to work with. An input of 'G', 'J' or P is accepted. This display only shows if the mode has not been set. When a file type has been chosen, a list of the files in that directory is displayed allowing the reader to choose the working file.
In order to work with files we have to import the java.io package. As the program can only listen for one key press, I had to limit the amount of files to 10. There is probably a way of accepting double figure inputs, but this chapter is on filters and the input code is off track, as it is.
In order for the pathnames to work on other operating systems I used the path separator attribute of the File class. Next I will have to allow a file to be chosen and opened in the system.

Code for Standalone File Choice:
import java.io.File;
String mode="none";
String dir="none";
boolean fileChosen=false;
int size=600;
File f;
PFont font;

void setup(){
size(size+size/3,size);
font=loadFont("Ang24.vlw");
textFont(font,24);
frameRate(20);}

void draw(){
if(!fileChosen){
if(mode=="none"){
chooseMode();}
else{
chooseFile();}}}

void clearDisplay(){
fill(255);
rect(0,0,width/2,height/2);
}

void chooseMode(){
display("Type of File\n G - GIF\n J - JPG\n P - PNG");
}

void chooseFile(){
String s=dir+" Files:\n";
String sep=File.separator;
String curDir = System.getProperty("user.dir");
String path=curDir+sep+"myWork"+sep+"ch79"+sep+"data"+sep+dir+sep;
File gDir=new File(path);
String[]files=gDir.list();
int i=files.length;
if(i>9)i=9;
for(int n=0;n<i;n++){
fname="files[n];
s+=n+": "+fName+"\n";}
s+="Enter File Number";
display(s);}

void display(String s){
clearDisplay();
fill(0);
text(s,2,20);}

void keyPressed(){
if(mode=="none"){
if(key='g'||key=='G'){mode="gif"){
dir="GIF";}
if(key=='j'||key=='J'){
mode="jpg";
dir="JPG";
}
if(key=='p'||key=='P'){
mode="png";
dir="PNG";
}}}

No comments:

Post a Comment