Thursday 19 March 2009

Load Chosen File

In order to load files from the data directories, make sure the permissions for ch79 are not set to read only. Navigate to the ch79 folder in myWork, right click,Properties and unclick the read only check box.
The application now loads the user's choice of file and displays it.
To set up the application, use the Tools/Create font utility to add the AngannaNew font with a size of 24. Change file name to Ang24. Make 3 new folders in the data folder for ch79. Name them,GIF, JPG and PNG.
The finished utility will not run as an applet as it loads and saves files on the client's PC. When the program is complete I will make it available for download. It gets the working directory from a system property. This, should work, but I have only tried it on my own pc.
Here is the code so far:
import java.io.File;
String mode="none";
String dir="none";
boolean fileChosen=false;
int size=600;
File f;
String sep=File.separator;
String curDir = System.getProperty("user.dir");
PFont font;
PImage i;
String[]files;
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();}}
else{
image(i,0,0,width,height);}
}

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 path=getPath();
File gDir=new File(path);
files=gDir.list();
int i=files.length;
if(i>9)i=9;
for(int n=0;n String fName=files[n];
s+=n+": "+fName+"\n";}
s+="Enter File Number";
display(s);}

void getFile(char key){

int num = Character.getNumericValue(key);
String f=files[num];
String path=getPath();
if(files.length>num+1){
i=loadImage(path+f);
fileChosen=true;
}}

String getPath(){
return curDir+sep+"myWork"+sep+"ch79"+sep+"data"+sep+dir+sep;

}

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";
}}
else{
if(key>='0'&&key<='9'){
print("choosing file");
getFile(key);}}
}

No comments:

Post a Comment