问题
my goal is to select a file from a "file open" dialog box, read it and draw objects based on the content of the file. I found a way to open that dialog box (see skeleton code below), however the PDE program starts drawing BEFORE I can select the file. As the drawing depends on the content of the selected file, I get a null pointer error.
My question is how can I select the file before the draw method starts ?
If I define my file (Amas.in) explicitely in setup(), all is well, the program shows my output based on the given file.
If I use selectInput(...), I only get the dialog box asking me for the file name AFTER draw() has started, resulting in the pointer error.
If I define my file explicitely and then call selectInput(...), the program starts drawing objects according to the initial file, then asks me which new file I want; however, after I select my new file, the program ignores the content of that new file.
Both the default Amas.in and my other file are in the data folder.
What am I doing wrong ?
Thanks in advance for any advice.
String myInputFile ;
void setup() {
selectInput("Select a file : ", "fileSelected");
String[] lines = loadStrings("Amas.in"); // works
//String[] lines = loadStrings(myInputFile); // doesn't work
}
void draw() {
ellipse(mouseX, mouseY, 9, 9);
println("Selected at this point " + myInputFile);
}
void fileSelected(File selection) {
if (selection == null) {
myInputFile = "Amas.in" ;
println("Default file is opened : " + myInputFile);
} else {
myInputFile = selection.getAbsolutePath() ;
println("User selected " + myInputFile);
}
}
回答1:
Maybe you can just check if your file is loaded in setup or even at draw, also i believe you want the declaration of lines outside setup, som thing like:
[CODE EDITED]
String [] myInputFileContents ;
String myFilePath;
void setup() {
selectInput("Select a file : ", "fileSelected");
while (myInputFileContents == null) {//wait
}
}
void draw() {
ellipse(mouseX, mouseY, 9, 9);
println("Selected at this point " + myFilePath);
}
void mousePressed() {
selectInput("Select a file : ", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("no selection so far...");
} else {
myFilePath = selection.getAbsolutePath();
myInputFileContents = loadStrings(myFilePath) ;// this moves here...
println("User selected " + myFilePath);
}
}
回答2:
The solution by v.k. is a proper one, since it uses the predefined methods in Processing, and eases better in the life of the program since it is allowed to run, just not display anything. It is (almost) always better to trust the provided framework instead of a hack, yet I will provide one as an alternative answer. This is especially useful if you want to pop the dialog before even setup() runs.
import javax.swing.*;
String myInputFile ;
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
void setup() {
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
myInputFile = file.getAbsolutePath();
}
else {
println("Cancelled.");
}
}
void draw() {
ellipse(mouseX, mouseY, 9, 9);
println("Selected at this point " + myInputFile);
}
来源:https://stackoverflow.com/questions/17665007/processing-2-0-open-file-dialog