I'm making a game with three main panels and a few subpanels, and I'm confused about how you "connect" the panels and their data.
I have my main class, which extends JFrame and adds three JPanels. Each of those panels is a subclass of JPanel. (Ex: JPanel gameControlPanel = new GameControlPanel(), where GameControlPanel is a class I created to extend JPanel.)
Now, all the game data (such as the game state, and two arraylists that hold saved players and saved scores) is in the game panel. But I need to get and set that data from the other two panels. And how to do so is evading me.
** So my question is: how do I do this? How can I access data in one JPanel subclass from another JPanel subclass (that have the same parent JFrame)?
If it helps, this is the extended JFrame class's code, where I add the three panels...:
JPanel controlButtonsPanel = new GameControlButtons();
controlButtonsPanel.setPreferredSize(new Dimension(801,60));
controlButtonsPanel.setBorder(new LineBorder(Color.white, 1));
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.weightx = 2;
constraints.weighty = 0.3;
this.add(controlButtonsPanel, constraints);
JPanel gameDataPanel = new GameDataPanel();
gameDataPanel.setPreferredSize(new Dimension(500,838));
gameDataPanel.setBorder(new LineBorder(Color.white, 2));
constraints.anchor = GridBagConstraints.NORTHEAST;
constraints.weightx = 1;
constraints.weighty = 2;
this.add(gameDataPanel, constraints);
JPanel graphicsPanel = new RoofRunnerGame("Ken");
constraints.anchor = GridBagConstraints.SOUTHWEST;
constraints.weightx = 2;
constraints.weighty = 1;
graphicsPanel.setBorder(new LineBorder(Color.white, 1));
graphicsPanel.setPreferredSize(new Dimension(800,800));
graphicsPanel.requestFocus();
this.add(graphicsPanel, constraints);
The graphicsPanel holds all of this data:
private ArrayList<Player> savedPlayers; // Holds saved data for player's who paused and exited game.
private ArrayList<Player> savedScores; // Holds high scores from player's who played game and died.
private ArrayList<Birds> birdList = new ArrayList<Birds>(); // Not serialized due to its randomness and unimportance to player.
private ArrayList<Clouds> cloudList = new ArrayList<Clouds>(); // Not serialized due to its randomness and unimportance to player.
private Player gamePlayer; // Player object that holds all data for a game instance.
And I want to access that data from inside the other two panels (gameDataPanel's class and gameControlButton's class).