We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Halli hallo,
I am having a bit of trouble with this piece of code.
PrintWriter output;
int savedTime;
int totalTime = 1000;
void setup() {
output = createWriter("positions.txt"); // Create a new file in the sketch directory
savedTime = millis();
}
void draw() {
point(mouseX, mouseY);
timer();
}
void timer()
{
int passedTime = millis() - savedTime;
// Has five seconds passed?
if (passedTime > totalTime) {
println( "data write~!" );
output.println(mouseX);
savedTime = millis(); // Save the current time to restart the timer!
}
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
The program basically takes the values of mouse X at certain time intervals. It then writes it in a .txt file in the directory UPON keyPressed() !!
My problem is, is that if the User exits the program via [ X ], the values are not saved. Is there anyway to go around this? I think there must be something very basic behind the solution but cannot wrap my head around it. Thank you for your time.
Answers
You can use stop() or exit(). See which one works for you.
I guess exit() is more guaranteed! And probably, flush() is redundant when using close()! :-?
P.S.: By guaranteed I've meant: to work than the stop() version! ^#(^
Thanks for the feedback. I will define my own methods instead of using the predefined ones.
Another question though. Is it possible to "save" although the User clicks close [ X ]? When when trying out, I found that pressing Close does not "save" the document.
EDIT: is that what the super.exit(); does?
EDIT 2: Thanks a lot guys. Indeed it was super.exit(); that saved the day. After implementing SilentAce's code the program automatically saves on exit.