I need a small help with arraylist (newbie)
in
Programming Questions
•
1 year ago
hello all,
I am trying not to draw a button top of another.
I draw first button. Everything is fine
I try to draw the second botton top of the first one. It does not let me. Everything is fine.
I try to draw the third button top of the second button. it does not let me. Everything is fine.
The problem is it remembers only the last button i draw. When i try to draw on top of the first button after drawing the third button; it let me do it.
What can i do that it will remember all the buttons.
Button class:
- class Node {
- int node_x; // x Position
int node_y; // y Position
int node_w; // width
int node_h; // height
String node_name; // name of the node
boolean mouseOverButton; // if mouse is over button
Node(int temp_node_x, int temp_node_y, int temp_node_number) {- node_x = temp_node_x;
node_y = temp_node_y;
node_w = 100;
node_h = 36;
node_name = Integer.toString(temp_node_number); //integer to string
mouseOverButton = false;
} - void display() {
rectMode(CENTER);
if (mouseOverButton) {
fill (YELLOW);
}else {
fill(MID_GRAY);
}
rect(node_x, node_y, node_w, node_h); //button
fill(BLACK); //text color black
text(node_name, node_x, node_y+6); //button text
rectMode(CORNER); // reset
} - boolean overButton() {
- if (mouseX >= node_x -50 && mouseX <= node_x + 50 && mouseY >= node_y -18 && mouseY <= node_y + 18) {
mouseOverButton = true;
//Returns true back to main program
return mouseOverButton;
}
else {
mouseOverButton = false;
//Returns false back to main program
return mouseOverButton;
}
}
MousePressed
- if (mouseY >= 25 && current_tab == "Node") {
if ( ((mouseX > 50) && (mouseY > 50)) && ((mouseX < 490) && (mouseY < 340)) ) { // Do not create the node edge of the window - for (int i = 0; i < listofNodes.size();i++) { // Loop through the ArrayList.
Node currentNode = (Node) listofNodes.get(i); // To be able to access the methods for the ArrayList element at the position 'i'
if (currentNode.overButton()) {
onit = true;
}
else {
onit = false;
}
}
if (!onit) {
listofNodes.add(new Node(mouseX, mouseY, nodecounter++)); // create a new button
}
} - }
1