Introduction
I put together an example Swing GUI to show how a multi-player game can be created where each player has their own JPanel
. A timer JPanel
appears in between showing the player JPanels
so you have time to switch seats.
Here's Player 1's JPanel
.
Here's the timer JPanel
, counting down.
Here's Player 2's JPanel
.
I didn't just change the JLabel
text. You're seeing two player JPanels
in one JFrame
.
Explanation
When I create a Swing GUI, I use the model / view / controller (MVC) pattern. This pattern allows me to separate my concerns and focus on one part of the Swing application at a time.
The MVC pattern using Java Swing works like this:
- The view reads information from the model.
- The view does not update the model.
- The controller updates the model and repaints / revalidates the view.
Usually, a Swing application has multiple controller classes, one for each action. This example has one ActionListener
since all we're doing is switching player JPanels
.
Model
This application has two model classes, Player
and GameState
. For now, the Player
class holds a player name. All other game information about a player, like score, goes in this class.
The GameState
class holds information about the game state. The List
of Player
instances tells us the number of players and the int
playerTurn
tells us whose turn it is.
View
This Swing GUI consists of a single JFrame
, with a main JPanel
using a CardLayout
. Each player has their own JPanel
. The JButton
at the bottom of the player JPanel
switches you to the next player.
Each player JPanel
contains whatever information from the GameState
class that a player needs to see to play the game. There can be other JButtons
on the player JPanel
that alter the state of the player. When the player finishes his turn, he clicks on the next turn JButton
to pass the game to the next player.
Controller
Right now, the only ActionListener
is the one that switches player JPanels
. There's an internal ActionListener
controlled by a Swing Timer
that allows players to switch seats. You can adjust the countdown time in the outer ActionListener
class. Other JButtons
would trigger other ActionListeners
.
Code
Here's the complete runnable example.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class MultiPlayerGame implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MultiPlayerGame());
}
private CardLayout cardLayout;
private GameState gameState;
private JLabel timerLabel;
private JPanel mainPanel;
private JPanel timerPanel;
private PlayerPanel[] playerPanels;
public MultiPlayerGame() {
this.gameState = new GameState();
}
@Override
public void run() {
JFrame frame = new JFrame("MultiPlayer Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.mainPanel = createMainPanel();
frame.add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
cardLayout = new CardLayout();
JPanel panel = new JPanel(cardLayout);
List<Player> players = gameState.getPlayers();
playerPanels = new PlayerPanel[players.size()];
for (int i = 0; i < players.size(); i++) {
Player player = players.get(i);
playerPanels[i] = new PlayerPanel(this, gameState,
player);
panel.add(playerPanels[i].getPanel(), player.getName());
}
timerPanel = createTimerPanel();
panel.add(timerPanel, "timer");
return panel;
}
private JPanel createTimerPanel() {
JPanel panel = new JPanel(new FlowLayout());
timerLabel = new JLabel(" ");
timerLabel.setFont(panel.getFont().deriveFont(16f));
panel.add(timerLabel);
return panel;
}
public void updateTimerPanel(Player player, int seconds) {
String text = "" + seconds + " seconds before " +
player.getName() + " may play";
timerLabel.setText(text);
}
public CardLayout getCardLayout() {
return cardLayout;
}
public JPanel getMainPanel() {
return mainPanel;
}
public JPanel getTimerPanel() {
return timerPanel;
}
public class PlayerPanel {
private final MultiPlayerGame frame;
private final GameState model;
private final JPanel panel;
public PlayerPanel(MultiPlayerGame frame, GameState model, Player player) {
this.frame = frame;
this.model = model;
this.panel = createPlayerPanel(player);
}
private JPanel createPlayerPanel(Player player) {
JPanel panel = new JPanel(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.setPreferredSize(new Dimension(300, 200));
JLabel label = new JLabel(player.getName());
label.setFont(panel.getFont().deriveFont(Font.BOLD, 24f));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label, BorderLayout.BEFORE_FIRST_LINE);
JButton button = new JButton("Next Player's Turn");
button.addActionListener(new ButtonListener(frame, model));
panel.add(button, BorderLayout.AFTER_LAST_LINE);
return panel;
}
public JPanel getPanel() {
return panel;
}
}
public class ButtonListener implements ActionListener {
private final MultiPlayerGame frame;
private final GameState model;
private Timer timer;
public ButtonListener(MultiPlayerGame frame, GameState model) {
this.frame = frame;
this.model = model;
}
@Override
public void actionPerformed(ActionEvent event) {
int delayPeriod = 30;
int turn = model.nextPlayerTurn();
Player player = model.getPlayers().get(turn);
CardLayout cardLayout = frame.getCardLayout();
timer = new Timer(1000, new ActionListener() {
private int delay = delayPeriod;
@Override
public void actionPerformed(ActionEvent innerEvent) {
--delay;
frame.updateTimerPanel(player, delay);
if (delay < 0) {
cardLayout.show(frame.getMainPanel(),
player.getName());
timer.stop();
}
}
});
timer.start();
frame.updateTimerPanel(player, delayPeriod);
cardLayout.show(frame.getMainPanel(), "timer");
}
}
public class GameState {
private int playerTurn;
private final List<Player> players;
public GameState() {
this.players = new ArrayList<>();
this.players.add(new Player("Player 1"));
this.players.add(new Player("Player 2"));
this.playerTurn = 0;
}
public int nextPlayerTurn() {
playerTurn = ++playerTurn % players.size();
return playerTurn;
}
public int getPlayerTurn() {
return playerTurn;
}
public List<Player> getPlayers() {
return players;
}
}
public class Player {
private final String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}