package de.tims.gameexplorer; import java.awt.*; import java.awt.event.*; import javax.swing.*; import de.tims.player_management.Player; import de.tims.player_management.PlayerManager; public class GameExplorer { private JFrame frame; private JPanel explorerPanel; private JPanel loginPanel; private JPanel gamePanel; private JPanel navigationPanel; private JPanel fleetstormPanel; private JPanel fourwinsPanel; private JPanel tictactoePanel; private JPanel leaderboardPanel; private JPanel border1; private JPanel border2; private JPanel border3; private JPanel border4; private JPanel border5; private JPanel border6; private JPanel border7; private JButton loginBtn; private JButton fleetstormBtn; private JButton fourwinsBtn; private JButton tictactoeBtn; private JButton leaderboardBtn; private JButton logoutBtn; private JButton exitBtn; private JButton backBtn; private JLabel username; private JLabel loginWarning; private JLabel playerName; private JLabel playerPoints; private JLabel actualPlayerName; private JLabel actualPlayerPoints; private JLabel chosenGame; private JTextField usernameInput; private Dimension minSize; private Dimension loginBtnSize; private Dimension btnSize; private Dimension smallBtnSize; private GridBagConstraints gbc; private static final String PLAYER_FILE = "src/main/java/resources/player_data.csv"; private enum Game { FLEETSTORM, FOURWINS, TICTACTOE, LEADERBOARD }; private Game actualGame; private PlayerManager manager; private Player actualPlayer; public GameExplorer() { manager = new PlayerManager(); frame = new JFrame("1000 infomagische Spiele"); minSize = new Dimension(480, 360); loginBtnSize = new Dimension(91, 20); btnSize = new Dimension(160, 40); smallBtnSize = new Dimension(75, 30); gbc = new GridBagConstraints(); buildLoginPanel(); buildNavigationPanel(); buildGamePanels(); frame.add(loginPanel); frame.setMinimumSize(minSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(640, 480); frame.setResizable(true); frame.setVisible(true); } private void buildExplorerPanel() { explorerPanel = new JPanel(); explorerPanel.setLayout(new GridBagLayout()); playerName = new JLabel("Spieler:"); playerPoints = new JLabel("Punkte:"); actualPlayerName = new JLabel(actualPlayer.getName()); actualPlayerPoints = new JLabel("" + actualPlayer.getPoints()); fleetstormBtn = new JButton("Schiffe versenken"); fleetstormBtn.setPreferredSize(btnSize); fleetstormBtn.addActionListener(new GameAction()); fourwinsBtn = new JButton("Vier gewinnt"); fourwinsBtn.setPreferredSize(btnSize); fourwinsBtn.addActionListener(new GameAction()); tictactoeBtn = new JButton("TicTacToe"); tictactoeBtn.setPreferredSize(btnSize); tictactoeBtn.addActionListener(new GameAction()); leaderboardBtn = new JButton("Leaderboard"); leaderboardBtn.setPreferredSize(btnSize); leaderboardBtn.addActionListener(new GameAction()); logoutBtn = new JButton("Logout"); logoutBtn.setPreferredSize(smallBtnSize); logoutBtn.addActionListener(new LogoutAction()); exitBtn = new JButton("Exit"); exitBtn.setPreferredSize(smallBtnSize); exitBtn.addActionListener(new ExitAction()); border1 = new JPanel(); border1.setOpaque(false); border2 = new JPanel(); border2.setOpaque(false); border3 = new JPanel(); border3.setOpaque(false); border4 = new JPanel(); border4.setOpaque(false); border5 = new JPanel(); border5.setOpaque(false); border7 = new JPanel(); border7.setOpaque(false); gbc.weightx = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(0, 0, 0, 0); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border1, gbc); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(fleetstormBtn, gbc); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border2, gbc); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(fourwinsBtn, gbc); gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border3, gbc); gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(tictactoeBtn, gbc); gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border4, gbc); gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 3; gbc.weighty = 0.0; explorerPanel.add(leaderboardBtn, gbc); gbc.gridx = 0; gbc.gridy = 8; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border5, gbc); gbc.gridx = 0; gbc.gridy = 9; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_END; gbc.insets = new Insets(0, 0, 0, 5); explorerPanel.add(logoutBtn, gbc); gbc.gridx = 2; gbc.gridy = 9; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_START; gbc.insets = new Insets(0, 5, 0, 0); explorerPanel.add(exitBtn, gbc); gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(0, 0, 0, 0); gbc.gridx = 0; gbc.gridy = 10; gbc.gridwidth = 3; gbc.weighty = 1.0 / 6; explorerPanel.add(border7, gbc); gbc.gridx = 0; gbc.gridy = 11; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_END; explorerPanel.add(playerName, gbc); gbc.gridx = 2; gbc.gridy = 11; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.insets = new Insets(0, 5, 0, 0); gbc.anchor = GridBagConstraints.LINE_START; explorerPanel.add(actualPlayerName, gbc); gbc.gridx = 0; gbc.gridy = 12; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_END; explorerPanel.add(playerPoints, gbc); gbc.gridx = 2; gbc.gridy = 12; gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.insets = new Insets(0, 5, 0, 0); gbc.anchor = GridBagConstraints.LINE_START; explorerPanel.add(actualPlayerPoints, gbc); } private void buildLoginPanel() { loginPanel = new JPanel(); loginPanel.setLayout(new GridBagLayout()); loginBtn = new JButton("Login"); loginBtn.setPreferredSize(loginBtnSize); loginBtn.addActionListener(new LoginAction()); username = new JLabel("Name eingeben:"); loginWarning = new JLabel(); usernameInput = new JTextField(8); gbc.gridwidth = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new Insets(0, 0, 5, 0); loginPanel.add(username, gbc); gbc.gridx = 0; gbc.gridy = 1; gbc.insets = new Insets(5, 0, 5, 0); loginPanel.add(usernameInput, gbc); gbc.gridx = 0; gbc.gridy = 2; gbc.insets = new Insets(5, 0, 5, 0); loginPanel.add(loginBtn, gbc); gbc.gridx = 0; gbc.gridy = 3; gbc.insets = new Insets(5, 0, 0, 0); loginPanel.add(loginWarning, gbc); } private void buildNavigationPanel() { navigationPanel = new JPanel(); navigationPanel.setLayout(new GridBagLayout()); backBtn = new JButton("< Zurück"); backBtn.addActionListener(new BackAction()); chosenGame = new JLabel(); border6 = new JPanel(); border6.setOpaque(false); gbc.gridwidth = 1; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0.0; gbc.insets = new Insets(5, 20, 5, 0); navigationPanel.add(backBtn, gbc); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1.0; gbc.insets = new Insets(0, 0, 0, 0); navigationPanel.add(border6, gbc); gbc.gridx = 2; gbc.gridy = 0; gbc.weightx = 0.0; gbc.insets = new Insets(5, 0, 5, 20); navigationPanel.add(chosenGame, gbc); } private void buildGamePanels() { gamePanel = new JPanel(); gamePanel.setLayout(new BorderLayout()); gamePanel.add(navigationPanel, BorderLayout.PAGE_START); //use of dummy panels because real panels have not been implemented yet fleetstormPanel = new JPanel(); fleetstormPanel.setBackground(Color.BLUE); fourwinsPanel = new JPanel(); fourwinsPanel.setBackground(Color.GREEN); tictactoePanel = new JPanel(); tictactoePanel.setBackground(Color.YELLOW); leaderboardPanel = new JPanel(); leaderboardPanel.setBackground(Color.RED); } private class LoginAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String userInput = usernameInput.getText(); if (!userInput.equals("")) { loginWarning.setText(""); manager.loadPlayers(PLAYER_FILE); actualPlayer = manager.selectPlayer(userInput); buildExplorerPanel(); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.remove(loginPanel); frame.add(explorerPanel); frame.revalidate(); frame.repaint(); } else { loginWarning.setText("Kein Name eingegeben!"); } } } private class GameAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //each button adds a different dummy panel to the gamePanel if (e.getSource() == fleetstormBtn) { actualGame = Game.FLEETSTORM; chosenGame.setText("Schiffe versenken"); gamePanel.add(fleetstormPanel, BorderLayout.CENTER); } else if (e.getSource() == fourwinsBtn) { actualGame = Game.FOURWINS; chosenGame.setText("Vier gewinnt"); gamePanel.add(fourwinsPanel, BorderLayout.CENTER); } else if (e.getSource() == tictactoeBtn) { actualGame = Game.TICTACTOE; chosenGame.setText("TicTacToe"); gamePanel.add(tictactoePanel, BorderLayout.CENTER); } else if (e.getSource() == leaderboardBtn) { actualGame = Game.LEADERBOARD; chosenGame.setText("Leaderboard"); gamePanel.add(leaderboardPanel, BorderLayout.CENTER); } frame.remove(explorerPanel); frame.add(gamePanel); frame.revalidate(); frame.repaint(); } } private class LogoutAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { manager.savePlayers(PLAYER_FILE); buildLoginPanel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.remove(explorerPanel); frame.add(loginPanel); frame.revalidate(); frame.repaint(); } } private class ExitAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { manager.savePlayers(PLAYER_FILE); System.exit(0); } } private class BackAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { switch (actualGame) { case FLEETSTORM: gamePanel.remove(fleetstormPanel); break; case FOURWINS: gamePanel.remove(fourwinsPanel); break; case TICTACTOE: gamePanel.remove(tictactoePanel); break; case LEADERBOARD: gamePanel.remove(leaderboardPanel); } actualPlayerPoints.setText("" + actualPlayer.getPoints()); frame.remove(gamePanel); frame.add(explorerPanel); frame.revalidate(); frame.repaint(); } } public static void main(String[] args) { new GameExplorer(); } }