处理2.0 - 打开文件对话框 [英] Processing 2.0 - Open file dialog
问题描述
。
我找到一种打开该对话框的方法(参见下面的框架代码),但是在我可以选择该文件之前,PDE程序启动
绘图。由于绘图取决于所选文件的
内容,所以我得到一个空指针错误。
我的问题是如何在绘制之前选择文件方法开始?
如果我在setup()中明确定义了我的文件(Amas.in),一切都很好,
程序显示了基于给定文件。
如果我使用selectInput(...),我只得到对话框,询问我的文件名
AFTER draw()已经启动,导致指针错误。
如果我明确定义了我的文件,然后调用selectInput(...),
程序将根据初始文件,然后要求我
我想要的新文件;然而,在我选择我的新文件之后,程序
会忽略该新文件的内容。
默认的Amas.in和我的其他文件都在数据文件夹。
我做错了什么?
提前感谢任何建议。
String myInputFile;
void setup(){
selectInput(选择一个文件:,fileSelected);
String [] lines = loadStrings(Amas.in); // works
// String [] lines = loadStrings(myInputFile); //不工作
}
void draw(){
ellipse(mouseX,mouseY,9,9);
println(此时选择+ myInputFile);
}
void fileSelected(文件选择){
if(selection == null){
myInputFile =Amas.in ;
println(默认文件被打开:+ myInputFile);
} else {
myInputFile = selection.getAbsolutePath();
println(用户选择+ myInputFile);
}
}
也许你可以检查你的文件是否在设置中加载,甚至是绘制,我也相信你想要在安装之外的声明,一些事情就像:
[CODE EDITED]
String [] myInputFileContents;
String myFilePath;
void setup(){
selectInput(Select a file:,fileSelected);
while(myInputFileContents == null){// wait
}
}
void draw(){
ellipse(mouseX,mouseY, 9);
println(此时选中+ myFilePath);
}
void mousePressed(){
selectInput(选择一个文件:,fileSelected);
}
void fileSelected(文件选择){
if(selection == null){
println(no selection to far ...);
} else {
myFilePath = selection.getAbsolutePath();
myInputFileContents = loadStrings(myFilePath); //这里移动...
println(用户选择+ myFilePath);
}
}
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);
}
}
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.0 - 打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!