Hi Everyone,
I am new to processing, and have a question, which is probably simple for many of you. I hope someone is willing to help me.
In the code below, you will notice at the bottom that a .txt file is being loaded. Suppose I have a whole library of .txt files named 1,2,3 and so forth. How do I load the next file (ie. 2 after 1, 3 after 2 etc) after the user presses a key?
The current code basically takes the text file, which contains coordinates for dots, and draws these dots. The effect I am after is after a user key press, a new drawing is made, because the code takes the data from 2.txt, key press, 3.txt, key press 4.txt and so forth...
/*
*** Our text file will look something like below, #
where each line corresponds to the x,y,z coordinate of a point!
coordX, coordY, coorzZ
coordX, coordY, coorzZ
coordX, coordY, coorzZ
... etc
*/
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
/*-----------------------------------------------------------------------------
*** GLOBAL INIT ***
-----------------------------------------------------------------------------*/
void setup(){
//-------------------------------- GENERAL
size(800,800);
background(0);
//-------------------------------- POINTS
pointList = new ArrayList(); // instantiate an ArrayList
importTextFile(); // call our custom function
}
/*-----------------------------------------------------------------------------
*** MAIN PROGRAM LOOP ***
-----------------------------------------------------------------------------*/
void draw(){
//-------------------------------- GENERAL
background(0);
stroke(210);
fill(210,90);
//-------------------------------- VISUALIZE THE POINT SET
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
ellipse(V.x,V.y, 20, 20);
}
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings("1.txt"); // the name and extension of the file to import!
for(int i = 0; i < strLines.length; ++i){
String[] arrTokens = split(strLines[i], ','); // use the split array with character to isolate each component
float xx = float(arrTokens[0]); // cast string value to a float values!
float yy = float(arrTokens[1]); // cast string value to a float values!
pointList.add( new PVector(xx,yy) ); // add values to a new array slot
}
}
I am new to processing, and have a question, which is probably simple for many of you. I hope someone is willing to help me.
In the code below, you will notice at the bottom that a .txt file is being loaded. Suppose I have a whole library of .txt files named 1,2,3 and so forth. How do I load the next file (ie. 2 after 1, 3 after 2 etc) after the user presses a key?
The current code basically takes the text file, which contains coordinates for dots, and draws these dots. The effect I am after is after a user key press, a new drawing is made, because the code takes the data from 2.txt, key press, 3.txt, key press 4.txt and so forth...
/*
*** Our text file will look something like below, #
where each line corresponds to the x,y,z coordinate of a point!
coordX, coordY, coorzZ
coordX, coordY, coorzZ
coordX, coordY, coorzZ
... etc
*/
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
/*-----------------------------------------------------------------------------
*** GLOBAL INIT ***
-----------------------------------------------------------------------------*/
void setup(){
//-------------------------------- GENERAL
size(800,800);
background(0);
//-------------------------------- POINTS
pointList = new ArrayList(); // instantiate an ArrayList
importTextFile(); // call our custom function
}
/*-----------------------------------------------------------------------------
*** MAIN PROGRAM LOOP ***
-----------------------------------------------------------------------------*/
void draw(){
//-------------------------------- GENERAL
background(0);
stroke(210);
fill(210,90);
//-------------------------------- VISUALIZE THE POINT SET
for(int i = 0; i < pointList.size(); ++i){
PVector V = (PVector) pointList.get(i);
ellipse(V.x,V.y, 20, 20);
}
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings("1.txt"); // the name and extension of the file to import!
for(int i = 0; i < strLines.length; ++i){
String[] arrTokens = split(strLines[i], ','); // use the split array with character to isolate each component
float xx = float(arrTokens[0]); // cast string value to a float values!
float yy = float(arrTokens[1]); // cast string value to a float values!
pointList.add( new PVector(xx,yy) ); // add values to a new array slot
}
}
1