Load .txt upon keypress OR every X seconds
in
Programming Questions
•
2 years ago
H Everyone,
In the code below, when a key is pressed, a new .txt file is loaded. My question is, how could I introduce added functionality whereby the new file is loaded after 5 seconds, say, even if no key has been pressed? To clarify, a new .txt is loaded after key press, but is loaded in any case after 5 seconds...
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
int currentFile = 1; // to keep track of the textfile, starts at 1
/*-----------------------------------------------------------------------------
*** 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);
}
}
void keyPressed() {
currentFile++;
pointList.clear();
importTextFile();
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings(currentFile + ".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
}
}
In the code below, when a key is pressed, a new .txt file is loaded. My question is, how could I introduce added functionality whereby the new file is loaded after 5 seconds, say, even if no key has been pressed? To clarify, a new .txt is loaded after key press, but is loaded in any case after 5 seconds...
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
ArrayList pointList; // arraylist to store the points in
int currentFile = 1; // to keep track of the textfile, starts at 1
/*-----------------------------------------------------------------------------
*** 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);
}
}
void keyPressed() {
currentFile++;
pointList.clear();
importTextFile();
}
/*-----------------------------------------------------------------------------
*** CUSTOM IMPORT FUNCTION ***
-----------------------------------------------------------------------------*/
void importTextFile(){
String[] strLines = loadStrings(currentFile + ".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