I'm modifying some existing Java code for an assignment and I can't figure out how to call a function of an existing object from within an ActionListener.
There will only ever be one instance of "myGame".
Here is the relevant code;
public class myGame extends JFrame {
public myGame() {
//...snip...
statsBar = new JLabel("");
add(statsBar, BorderLayout.SOUTH);
add(new Board(statsBar));
setResizable(false);
setVisible(true);
addMenubar();
}
private void addMenubar() {
JMenuBar menubar = new JMenuBar();
JMenu topMnuGame = new JMenu("File");
JMenuItem mnuSolve = new JMenuItem("Solve");
mnuSolve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// freshGame.solveGame();
// this is where I need to call the solveGame function
// for the "freshGame" instance.
solveGame();
}
});
topMnuGame.add(mnuSolve);
menubar.add(topMnuGame);
}
public static void main(String[] args) {
myGame freshGame = new myGame();
}
}
.
public class Board extends JPanel {
public Board(JLabel statsBar) {
this.statsBar = statsBar;
//..snip..
addMouseListener( new gameAdapter() );
}
public void solveGame() {
// .. do stuff with object ..
}
}
So my question is, how can I call "solveGame()" from within the "myGame" class using the "freshGame" instance?