We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Can I call a class from within a class
Page Index Toggle Pages: 1
Can I call a class from within a class? (Read 870 times)
Can I call a class from within a class?
Sep 4th, 2009, 12:26am
 
I have this learning experiment. It is a button that opens a menu with a second button on the menu. The second button opens a sub-menu.

All of this is within one class. But what if I wanted to have 10 sub-menus and their associated buttons on this menu and in addition, adding data and functions for (what ever) to each sub-menu.

This would be one huge class, which I think would defeat the purpose of organizing the code into a class in the first place.

In a tree like structure such as this, is each branch a class within the class of the tree as a whole or are they just objects? How is such a structure organized?

Thanks.
Re: Can I call a class from within a class?
Reply #1 - Sep 4th, 2009, 1:25am
 
In AWT, for example (it is similar in Swing), you create a MenuBar, and register Menu objects to it (File, Options, Help, etc.). Each Menu has MenuItem objects added to it, and you add an action listener to each MenuItem. These ActionListener objects are actually (generally anonymous) classes following the interface which defines only an actionPerformed method to be overridden to do the menu action.
So it is strongly hierarchical, yet quite flexible.
When you click on a menu item, it looks at its list of action listeners, and call actionPerformed method on each object (generally only one!) it finds.
Re: Can I call a class from within a class?
Reply #2 - Sep 4th, 2009, 10:57am
 
Thanks. That is good to know for the future. I looked AWT up on Wiki. and book marked it for future reference.

My goal at the moment is less about the creation of menus and more about learning how to organize code into objects and classes. My one class is going to get very long and difficult to read. I thought maybe it could be divided up and placed on separate tabs.

Do you think AWT will display the code for me to analyze?
Or is there another way for a beginner like me?

Re: Can I call a class from within a class?
Reply #3 - Sep 4th, 2009, 11:56am
 
For the record, you can look at the API at Menu and related.
The source code is available if you download a JDK (the big src.zip file at the root) or can be downloaded at the Open JDK site as Bz2, Zip or Gz file.

But there isn't much to them, mostly an organization of hierarchy.
Note that AWT relies on the system (Windows, MacOS, etc.) to display the components, while Swing draws them, a bit like controlP5 does.
Re: Can I call a class from within a class?
Reply #4 - Sep 4th, 2009, 2:02pm
 
Thanks. I will try to down load and have a look at that menu code. I booked marked both those sites and will likely refer to them in the future more after I get some experience with processing.

I don't think I have ever seen any large or sophisticated program made with processing. I wish there was a site where people posted their processing programs to be down loaded along with the source code so people could learn from each other.

Maybe it will help to illustrate the growing complexity of my one class if I post the code. By the way; Blindfish helped me with the creation of the sub-menu.

If I were to add a few more sub-menus or a sub-sub-menus, this one class would get very confusing to work on. I thought I could combine some of this into separate objects or classes and store them on different tabs.

Menu menu;

boolean button = false;
boolean submenu = false;

void setup() {
 size(300,300);
 menu = new Menu();
}

void draw() {
 background(180);
 menu.display_gui();
 menu.display_menu();
}

void mouseClicked() {
 menu.mouseClicked();
}

class Menu {
 
 void display_gui() {  //upper blue button
    fill (140,160,220);
    if (mouseX > 10 && mouseX < 110 && mouseY > 10 && mouseY < 30) {
       fill(150,190,250);
    }
    rect(10,10,100,20);
 }

 void display_menu() {
     if (button) {
         fill(255);
         rect(10,30,100,150);   // white menu
         fill (140,160,220);
         if (mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120) {
             fill(150,190,250);
         }
         rect(10,100,100,20);  // lower blue button
         
         // check that the mouse is hovering over the second button
         if (mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120) {
            // if it is set the boolean variable to true
            submenu = true;
         }
         
         // If the boolean is true we can display the submenu
         if (submenu) {
               fill(255);
               rect(110,100,100,150);
               
               if (! (mouseX>=110 && mouseX<210 && mouseY>100 && mouseY<250 || mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120) ){
                   // If it does we set the boolean to false and the submenu is hidden
                   submenu = false;
               }
         }
     }

 void mouseClicked() {
     if (mouseX > 10 && mouseX < 110 && mouseY > 10 && mouseY < 30) {
         button = !button;
     }
 }
}

Page Index Toggle Pages: 1