I just finished my first course in Java programming (I'm a Biomedical Engineering major, but wanted to play with code a bit more). I have a big question that has been taunting me throughout the class that I've wanted to get off my chest. I'm going to set up a hypothetical situation to make my question easier to understand.
I am creating a game. I have the driver class (game.java) which sets up the JFrame and has the method that gets looped over and over. It also creates and adds two classes that extend JPanel (scorePanel and gamePanel). Score panel is a pane with a start and stop button, a score JTextField, and some labels. The gamePanel is a panel in which .. tetris blocks are redrawn over and over.
Now my question is.. How can I have each class call methods of other classes when they don't have the reference to the other two objects. (remember only game.java has references to the two panel objects). I know it's possible to use static methods, but then there's always so much hassle with static methods referencing non-static variables and.. just a whole bunch of issues to work around. I keep thinking to myself.. "God there is an easier way!"
Any insight on this? Or does this basically boil down to "Don, you gotta work on your OO-design."
I'd appreciate any insight. I'm a beginner, but a weathered one. This could easily be a concept I just failed to fully grasp.
In the scenario which you mentioned, the top level ancestor will be the JFrame instance. Your can have your frame provide pass through methods so that the child components will be blissfully unaware of each other, but will still be able to communicate to each other via the JFrame.