Problem: Saving different comments by multiple users to later access them randomly.
in
Programming Questions
•
1 years ago
I have created a box in which users can type in their comments as a response to an image I will show them on the screen.
My objective is to accumulate all the different users comments (sentences) in a text file and then later, randomly load some of the words in each user's comments and paste them on the canvas in random locations (so a bit like an "exquisite cadaver") to create a poetic word association.
Currently, I can save one comment in a text file by typing in my box and pressing RETURN, but if I type another comment, the first is overwritten by the second one (so I lose the first one).
I am assuming that the solution is to use an array list. Am I correct and if so, what direction should I take with this strategy to achieve my goal?
Here is my code so far:
TextField textbox;
void setup() {
size (screen.width, screen.height);
textbox=new TextField (100, height-200, 400, 100, color (100));
}
void draw() {
background (204);
textbox.display();
textbox.rollOver();
}
// mouse and keyboard events
void mousePressed () {
textbox.press(mouseX, mouseY);
}
void mouseDragged() {
textbox.drag (mouseX, mouseY);
}
void mouseReleased () {
textbox.release ();
}
void keyPressed () {
textbox.keyPressed();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
class TextField extends InteractiveBoxes {
PFont fontA;
String userInputText= ""; // declare and initialize the variable to store text while it is being typed
String captureText= ""; // declare and initialize the variable to save typed text when return key is hit
String[] fullComment;
int spacing;
int fontHeight;
color boxClr;
// constructor of the TextField class
TextField (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color tempBoxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
fontA = loadFont("ArialMT-48.vlw");
boxClr=tempBoxClr;
spacing = 5;
}
void display () {
// Display everything
fill (boxClr);
strokeWeight(4);
strokeJoin(ROUND);
rect (boxPosX, boxPosY, boxPosW, boxPosH);
fontHeight=16; // set the font size
textFont(fontA, fontHeight); // Set the font type and size at Arial 14 for text instructions
fill(240); // Set the fill for text instructions
text("Type a few words that describe your impression. \nHit return to save what you typed. ", boxPosX+spacing, boxPosY+fontHeight);
fontHeight=20; // reset the font size
fill (255); //set the fill for the user's input text
textFont(fontA, fontHeight); // Set the font type and size at Arial 20 for text instructions
text(userInputText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
text(captureText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
}
void keyPressed() {
fullComment = splitTokens(captureText, ", ");
userInputText = userInputText + key; // add each character typed by the user to the end of the userInputText String variable.
// If the return key is pressed, save the String and clear it
if (key == RETURN || key == ENTER) {
captureText = userInputText;
saveStrings("userCommentsFile.txt", fullComment);
userInputText = ""; // Clear the String by setting it equal to ""
}
}
}
My objective is to accumulate all the different users comments (sentences) in a text file and then later, randomly load some of the words in each user's comments and paste them on the canvas in random locations (so a bit like an "exquisite cadaver") to create a poetic word association.
Currently, I can save one comment in a text file by typing in my box and pressing RETURN, but if I type another comment, the first is overwritten by the second one (so I lose the first one).
I am assuming that the solution is to use an array list. Am I correct and if so, what direction should I take with this strategy to achieve my goal?
Here is my code so far:
TextField textbox;
void setup() {
size (screen.width, screen.height);
textbox=new TextField (100, height-200, 400, 100, color (100));
}
void draw() {
background (204);
textbox.display();
textbox.rollOver();
}
// mouse and keyboard events
void mousePressed () {
textbox.press(mouseX, mouseY);
}
void mouseDragged() {
textbox.drag (mouseX, mouseY);
}
void mouseReleased () {
textbox.release ();
}
void keyPressed () {
textbox.keyPressed();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
class TextField extends InteractiveBoxes {
PFont fontA;
String userInputText= ""; // declare and initialize the variable to store text while it is being typed
String captureText= ""; // declare and initialize the variable to save typed text when return key is hit
String[] fullComment;
int spacing;
int fontHeight;
color boxClr;
// constructor of the TextField class
TextField (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color tempBoxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
fontA = loadFont("ArialMT-48.vlw");
boxClr=tempBoxClr;
spacing = 5;
}
void display () {
// Display everything
fill (boxClr);
strokeWeight(4);
strokeJoin(ROUND);
rect (boxPosX, boxPosY, boxPosW, boxPosH);
fontHeight=16; // set the font size
textFont(fontA, fontHeight); // Set the font type and size at Arial 14 for text instructions
fill(240); // Set the fill for text instructions
text("Type a few words that describe your impression. \nHit return to save what you typed. ", boxPosX+spacing, boxPosY+fontHeight);
fontHeight=20; // reset the font size
fill (255); //set the fill for the user's input text
textFont(fontA, fontHeight); // Set the font type and size at Arial 20 for text instructions
text(userInputText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
text(captureText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
}
void keyPressed() {
fullComment = splitTokens(captureText, ", ");
userInputText = userInputText + key; // add each character typed by the user to the end of the userInputText String variable.
// If the return key is pressed, save the String and clear it
if (key == RETURN || key == ENTER) {
captureText = userInputText;
saveStrings("userCommentsFile.txt", fullComment);
userInputText = ""; // Clear the String by setting it equal to ""
}
}
}
1