Creating a Class within a Class

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

  • Answer ✓

    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

  • edited March 2016

    You can have classes within classes but not within methods like you have there.

    • Java indeed allows classes within methods. But not methods within methods though.
    • However classes defined within a method is kinda pointless and practically nonexistent in Java's ecosystem.
    • That is so b/c a class defined within a method only exists there and nowhere else.
    • Unless you wish for checkPress() to be the only place for class Menu to be used, you're much better off defining it outside any methods. :-@
  • 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?

  • edited March 2016

    So if I nest these classes, should I call them all in the draw loop?

    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:-)

Sign In or Register to comment.