Hi again all. Having continued with my project I have managed to finish my Board() which is where my main game occurs. I have also created a main menu which is instantiated from my main() method and appears as soon as the program is run.
My current problem is invoking the Board() and adding it to the JFrame when my MouseListener is triggered from my MainMenu. Both Board and MainMenu inherit JPanel, so effectively, I'm trying to replace a JPanel based on the first panel's MouseListener.
I've tried to add a getInvoker() method to my MainMenu which is set using the class' constructor. This, along with a type cast has allowed me to target methods on MainClass from MainMenu. Although the message dialog that I've entered to appear when the Board is invoked, I can only see a grey JFrame when Board is instantiated in this way.
Here's the method that I've added to my MainClass to instantiate a Board JPanel.
public void addBoard(){
Board board = new Board();
add(board);
}
Here's the getInvoker() method I've added to my MainMenu.
private JFrame _invoker;
public JFrame getInvoker(){
return _invoker;
}
public void setInvoker(JFrame _invoker){
this._invoker = _invoker;
}
Here's where I've set the invoker variable in the MainMenu constructor. Note that I have specified the invoker when the class' constructor is called using MainMenu mainMenu = new MainMenu(this); from MainClass.
public MainMenu(JFrame invoker){ //Constructor
...
...
setInvoker(invoker);
And here's the method callback from MainMenu that is triggered by the class' mouse listener.
private void switchToBoard(){ //Changes the Menu for Board
this.setVisible(false);
((MainClass) this.getInvoker()).addBoard();
}
Sorry if this doesn't make much sense, but I appreciate it if someone would try and follow what I'm trying to do. If anyone has questions or needs more code examples, please let me know.
Thanks!
Morley