Hello everyone, my doubt is this: I have to make a program that saves the results of pressing a button, in Csv format.
For example: the program starts, and two buttons appear, true and false, I can’t understand how to save the result in csv based on the buttons I press, like, if I pressed the “true” button in the csv I must have 1 in the True column, and 0 in the False column.
That’s my code:
class Button{
float x,y; //position
float w,h; //size
boolean selected; //is the button selected / on? true/false
color selectedColor, defaultColor, currentColor;
String label;
///CONSTRUCTORS - no return type declared - match Class-name
Button(float x, float y, float w, float h, String label ){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
selected = false;
selectedColor = color( 280, 100, 100);
defaultColor = color( 280, 70, 70);
currentColor = defaultColor;
}
///METHODS
void display(){
fill( currentColor);
rect( x, y, w, h);
fill( 0);//black for text
textAlign(CENTER);
text( label, x + w/2, y + (h/2));
}
void clicked( int mx, int my){
if( mx > x && mx < x + w && my > y && my < y+h){
//mouse has been clicked
selected = !selected;
if( selected){
currentColor = selectedColor;
println("True");
}else{
currentColor = defaultColor;
}
}
}
}
class Button2{
float x,y; //position
float w,h; //size
boolean selected; //is the button selected / on? true/false
color selectedColor, defaultColor, currentColor;
String label;
///CONSTRUCTORS - no return type declared - match Class-name
Button2(float x, float y, float w, float h, String label ){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
selected = false;
selectedColor = color( 280, 100, 100);
defaultColor = color( 280, 70, 70); /
currentColor = defaultColor;
}
void display(){
fill( currentColor);
rect( x, y, w, h);
fill( 0);//black for text
textAlign(CENTER);
text( label, x + w/2, y + (h/2));
}
void clicked( int mx, int my){
if( mx > x && mx < x + w && my > y && my < y+h){
//mouse has been clicked
selected = !selected; //toggle the value between true and false
if( selected){
currentColor = selectedColor;
println("False");
}else{
currentColor = defaultColor;
}
}
}
} Button btn1;
Button2 b2;//data-type, variable-name //null
int x; //data-type, variable-name //0
Table table;
///Initialize things - variables
void setup(){
size( 600, 600);
colorMode(HSB, 360, 100, 100);
x= 10; //initialize the variable
btn1 = new Button( 10, 10, 100, 100, "True" ); //initialize by calling a Button constructor
b2 = new Button2(200,10, 100, 100, "False");
}
void draw( ){
btn1.display(); dù
b2.display();
table = new Table();
table.addColumn("True");
table.addColumn("False");
TableRow newRow = table.addRow();
newRow.setInt("True", table.getRowCount() - 1);
saveTable(table,"data/new.csv");
}
void mouseClicked( ){
btn1.clicked( mouseX, mouseY);
b2.clicked(mouseX,mouseY);
}