Read the values of variables from a TXT file in Processing

大城市里の小女人 提交于 2019-12-11 18:13:18

问题


I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.

static final int ribbon_length = 255, H = 200; 

void setup() {
  size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
  float p = 1;
  int up_y = 10;
  int widthh = 1;
  int height = 180;
  float a = pow (ribbon_length, 1-p);
  float colour = 0;
  for (int step = 0; step <= 255; step++) { 
      colour = a * pow (step, p);
      fill(colour,0,0); 
      rect(widthh*step, up_y, widthh, height);
      noStroke();
   }
}

The values tha I want to read from the txt is

  float p = 1; 
  int up_y = 10; 
  int widthh = 1;
  int height = 180;

I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...

BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
  size(ribbon_length, H);
  reader = createReader("positions.txt");  
}
.
.
.
.

Any ideas????

EDIT: Thanks for your answers. I try some changes after your commends. But It didn't show any color.

static final int ribbon_length = 255, H = 200; 

void setup() {
  size(ribbon_length, H);
}

void draw() {
  String[] lines = loadStrings("input.txt");
  float p = float(split(lines[0], "=")[1]);
  int up_y = int(split(lines[1], "=")[1]);
  int wide = int(split(lines[2], "=")[1]);
  int high = int(split(lines[3], "=")[1]);
  float a = pow (ribbon_length, 1-p);
  float colour = 0;
  for (int step = 0; step <= 255; step++) { 
      colour = a * pow (step, p);
      fill(colour,0,0); 
      rect(wide*step, up_y, wide, high);
      noStroke();
   }
}

回答1:


You can have a plain txt where each of the lines is a variable, so you can use the BufferedReader like this.

BufferedReader br = new BufferedReader(new FileReader("fileName.txt"));

Then you read each line with br.readLine() and then you finally assign the read line to the variable, transforming it to the necessary type. For example, if your line is an int you would have to do

int myInt = Integer.parseInt(br.readLine())

You can also have a properties file, in which every line has the format "key = value". You can try this webpage to load that properties file: http://www.mkyong.com/java/java-properties-file-examples/ (check part 2: Loading a properties file)




回答2:


First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.

The simplest way to do it is to have a bare text file like this:

1
10
1
180

exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.

void setup(){
  ....
  String[] lines = loadStrings("positions.txt");
  float p = float(lines[0]);
  int up_y = int(lines[1]);
  int wide = int(lines[2]);
  int high = int(lines[3]);
  ....
}

This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:

text file:

p=1
up_y=10

processing code:

String[] lines = loadStrings("input.txt")
float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
int up_y = int(split(lines[1], "=")[1]);

If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");



来源:https://stackoverflow.com/questions/21815517/read-the-values-of-variables-from-a-txt-file-in-processing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!