We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello!
I'm trying to create a class to use for a recursive interface program. Basically I'd like to wrap up a "button" class, a "menu" class, a "menu button" class, and "submenu" class. The program will start with an initial drop down menu that displays a series of buttons that open an endless number of submenus.
Right now I'm having an issue with where to call the menu class within the initial button class. Also I'm not really sure if nesting classes in this way is the most flexible/sensible way to make this program.
here is what I have:
boolean buttonPress = false;
button b1 = new button(10, 10, 100, 20);
void setup() {
size(600, 500);
}
void draw() {
background(200);
b1.checkPress();
}
class button {
int buttonH, buttonW;
int buttonX, buttonY;
button (int bH, int bW, int bX, int bY) {
buttonH = bH;
buttonW = bW;
buttonX = bX;
buttonY = bY;
}
void checkPress() {
fill (100, 150, 200);
if (mouseX > buttonX && mouseX < (buttonX + buttonW) &&
mouseY > buttonY && mouseY < (buttonY + buttonH) &&
mousePressed) {
fill(150, 200, 250);
buttonPress = !buttonPress;
}
rect(buttonX, buttonY, buttonW, buttonH);
if (buttonPress) {
menu m1 = new menu(10, 30, 100, 150);
menu.dropMenu();
class menu {
int menuH, menuW;
int menuX, menuY;
menu (int mH, int mW, int mX, int mY) {
menuH = mH;
menuW = mW;
menuX = mX;
menuY = mY;
}
void dropMenu() {
fill(255);
rect(menuX, menuY, menuW, menuH);
fill (255, 160, 0);
}
}
}
}
}
Answers
the java standard is that classes start with a capital letter - Button rather than button.
you can have classes within classes but not within methods like you have there. (in fact all processing classes are inner classes by default because of the way the preprocessor wraps sketch code)
official java tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
So if I nest these classes, should I call them all in the draw loop? also should I declare the variables for all of the classes in the initial class? Is there a more straight forward way to have these interface elements interact in a reproduceable way?
All classes & interfaces defined within a ".pde" tab file are nested already.
Just define them outside any functions/methods and you're good to go. O:-)