You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
11 KiB

  1. package de.tims.gameexplorer;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import de.tims.player_management.Player;
  6. import de.tims.player_management.PlayerManager;
  7. public class GameExplorer {
  8. private JFrame frame;
  9. private JPanel explorerPanel;
  10. private JPanel loginPanel;
  11. private JPanel gamePanel;
  12. private JPanel navigationPanel;
  13. private JPanel fleetstormPanel;
  14. private JPanel fourwinsPanel;
  15. private JPanel tictactoePanel;
  16. private JPanel leaderboardPanel;
  17. private JPanel border1;
  18. private JPanel border2;
  19. private JPanel border3;
  20. private JPanel border4;
  21. private JPanel border5;
  22. private JPanel border6;
  23. private JPanel border7;
  24. private JButton loginBtn;
  25. private JButton fleetstormBtn;
  26. private JButton fourwinsBtn;
  27. private JButton tictactoeBtn;
  28. private JButton leaderboardBtn;
  29. private JButton logoutBtn;
  30. private JButton exitBtn;
  31. private JButton backBtn;
  32. private JLabel username;
  33. private JLabel loginWarning;
  34. private JLabel playerName;
  35. private JLabel playerPoints;
  36. private JLabel actualPlayerName;
  37. private JLabel actualPlayerPoints;
  38. private JLabel chosenGame;
  39. private JTextField usernameInput;
  40. private Dimension minSize;
  41. private Dimension loginBtnSize;
  42. private Dimension btnSize;
  43. private Dimension smallBtnSize;
  44. private GridBagConstraints gbc;
  45. private static final String PLAYER_FILE = "src/main/java/resources/player_data.csv";
  46. private enum Game { FLEETSTORM, FOURWINS, TICTACTOE, LEADERBOARD };
  47. private Game actualGame;
  48. private PlayerManager manager;
  49. private Player actualPlayer;
  50. public GameExplorer() {
  51. manager = new PlayerManager();
  52. frame = new JFrame("1000 infomagische Spiele");
  53. minSize = new Dimension(480, 360);
  54. loginBtnSize = new Dimension(91, 20);
  55. btnSize = new Dimension(160, 40);
  56. smallBtnSize = new Dimension(75, 30);
  57. gbc = new GridBagConstraints();
  58. buildLoginPanel();
  59. buildNavigationPanel();
  60. buildGamePanels();
  61. frame.add(loginPanel);
  62. frame.setMinimumSize(minSize);
  63. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. frame.setSize(640, 480);
  65. frame.setResizable(true);
  66. frame.setVisible(true);
  67. }
  68. private void buildExplorerPanel() {
  69. explorerPanel = new JPanel();
  70. explorerPanel.setLayout(new GridBagLayout());
  71. playerName = new JLabel("Spieler:");
  72. playerPoints = new JLabel("Punkte:");
  73. actualPlayerName = new JLabel(actualPlayer.getName());
  74. actualPlayerPoints = new JLabel("" + actualPlayer.getPoints());
  75. fleetstormBtn = new JButton("Schiffe versenken");
  76. fleetstormBtn.setPreferredSize(btnSize);
  77. fleetstormBtn.addActionListener(new GameAction());
  78. fourwinsBtn = new JButton("Vier gewinnt");
  79. fourwinsBtn.setPreferredSize(btnSize);
  80. fourwinsBtn.addActionListener(new GameAction());
  81. tictactoeBtn = new JButton("TicTacToe");
  82. tictactoeBtn.setPreferredSize(btnSize);
  83. tictactoeBtn.addActionListener(new GameAction());
  84. leaderboardBtn = new JButton("Leaderboard");
  85. leaderboardBtn.setPreferredSize(btnSize);
  86. leaderboardBtn.addActionListener(new GameAction());
  87. logoutBtn = new JButton("Logout");
  88. logoutBtn.setPreferredSize(smallBtnSize);
  89. logoutBtn.addActionListener(new LogoutAction());
  90. exitBtn = new JButton("Exit");
  91. exitBtn.setPreferredSize(smallBtnSize);
  92. exitBtn.addActionListener(new ExitAction());
  93. border1 = new JPanel();
  94. border1.setOpaque(false);
  95. border2 = new JPanel();
  96. border2.setOpaque(false);
  97. border3 = new JPanel();
  98. border3.setOpaque(false);
  99. border4 = new JPanel();
  100. border4.setOpaque(false);
  101. border5 = new JPanel();
  102. border5.setOpaque(false);
  103. border7 = new JPanel();
  104. border7.setOpaque(false);
  105. gbc.weightx = 0.0;
  106. gbc.anchor = GridBagConstraints.CENTER;
  107. gbc.insets = new Insets(0, 0, 0, 0);
  108. gbc.gridx = 0;
  109. gbc.gridy = 0;
  110. gbc.gridwidth = 3;
  111. gbc.weighty = 1.0 / 6;
  112. explorerPanel.add(border1, gbc);
  113. gbc.gridx = 0;
  114. gbc.gridy = 1;
  115. gbc.gridwidth = 3;
  116. gbc.weighty = 0.0;
  117. explorerPanel.add(fleetstormBtn, gbc);
  118. gbc.gridx = 0;
  119. gbc.gridy = 2;
  120. gbc.gridwidth = 3;
  121. gbc.weighty = 1.0 / 6;
  122. explorerPanel.add(border2, gbc);
  123. gbc.gridx = 0;
  124. gbc.gridy = 3;
  125. gbc.gridwidth = 3;
  126. gbc.weighty = 0.0;
  127. explorerPanel.add(fourwinsBtn, gbc);
  128. gbc.gridx = 0;
  129. gbc.gridy = 4;
  130. gbc.gridwidth = 3;
  131. gbc.weighty = 1.0 / 6;
  132. explorerPanel.add(border3, gbc);
  133. gbc.gridx = 0;
  134. gbc.gridy = 5;
  135. gbc.gridwidth = 3;
  136. gbc.weighty = 0.0;
  137. explorerPanel.add(tictactoeBtn, gbc);
  138. gbc.gridx = 0;
  139. gbc.gridy = 6;
  140. gbc.gridwidth = 3;
  141. gbc.weighty = 1.0 / 6;
  142. explorerPanel.add(border4, gbc);
  143. gbc.gridx = 0;
  144. gbc.gridy = 7;
  145. gbc.gridwidth = 3;
  146. gbc.weighty = 0.0;
  147. explorerPanel.add(leaderboardBtn, gbc);
  148. gbc.gridx = 0;
  149. gbc.gridy = 8;
  150. gbc.gridwidth = 3;
  151. gbc.weighty = 1.0 / 6;
  152. explorerPanel.add(border5, gbc);
  153. gbc.gridx = 0;
  154. gbc.gridy = 9;
  155. gbc.gridwidth = 1;
  156. gbc.weighty = 0.0;
  157. gbc.anchor = GridBagConstraints.LINE_END;
  158. gbc.insets = new Insets(0, 0, 0, 5);
  159. explorerPanel.add(logoutBtn, gbc);
  160. gbc.gridx = 2;
  161. gbc.gridy = 9;
  162. gbc.gridwidth = 1;
  163. gbc.weighty = 0.0;
  164. gbc.anchor = GridBagConstraints.LINE_START;
  165. gbc.insets = new Insets(0, 5, 0, 0);
  166. explorerPanel.add(exitBtn, gbc);
  167. gbc.anchor = GridBagConstraints.CENTER;
  168. gbc.insets = new Insets(0, 0, 0, 0);
  169. gbc.gridx = 0;
  170. gbc.gridy = 10;
  171. gbc.gridwidth = 3;
  172. gbc.weighty = 1.0 / 6;
  173. explorerPanel.add(border7, gbc);
  174. gbc.gridx = 0;
  175. gbc.gridy = 11;
  176. gbc.gridwidth = 1;
  177. gbc.weighty = 0.0;
  178. gbc.anchor = GridBagConstraints.LINE_END;
  179. explorerPanel.add(playerName, gbc);
  180. gbc.gridx = 2;
  181. gbc.gridy = 11;
  182. gbc.gridwidth = 1;
  183. gbc.weighty = 0.0;
  184. gbc.insets = new Insets(0, 5, 0, 0);
  185. gbc.anchor = GridBagConstraints.LINE_START;
  186. explorerPanel.add(actualPlayerName, gbc);
  187. gbc.gridx = 0;
  188. gbc.gridy = 12;
  189. gbc.gridwidth = 1;
  190. gbc.weighty = 0.0;
  191. gbc.anchor = GridBagConstraints.LINE_END;
  192. explorerPanel.add(playerPoints, gbc);
  193. gbc.gridx = 2;
  194. gbc.gridy = 12;
  195. gbc.gridwidth = 1;
  196. gbc.weighty = 0.0;
  197. gbc.insets = new Insets(0, 5, 0, 0);
  198. gbc.anchor = GridBagConstraints.LINE_START;
  199. explorerPanel.add(actualPlayerPoints, gbc);
  200. }
  201. private void buildLoginPanel() {
  202. loginPanel = new JPanel();
  203. loginPanel.setLayout(new GridBagLayout());
  204. loginBtn = new JButton("Login");
  205. loginBtn.setPreferredSize(loginBtnSize);
  206. loginBtn.addActionListener(new LoginAction());
  207. username = new JLabel("Name eingeben:");
  208. loginWarning = new JLabel();
  209. usernameInput = new JTextField(8);
  210. gbc.gridwidth = 1;
  211. gbc.weightx = 0.0;
  212. gbc.weighty = 0.0;
  213. gbc.anchor = GridBagConstraints.CENTER;
  214. gbc.gridx = 0;
  215. gbc.gridy = 0;
  216. gbc.insets = new Insets(0, 0, 5, 0);
  217. loginPanel.add(username, gbc);
  218. gbc.gridx = 0;
  219. gbc.gridy = 1;
  220. gbc.insets = new Insets(5, 0, 5, 0);
  221. loginPanel.add(usernameInput, gbc);
  222. gbc.gridx = 0;
  223. gbc.gridy = 2;
  224. gbc.insets = new Insets(5, 0, 5, 0);
  225. loginPanel.add(loginBtn, gbc);
  226. gbc.gridx = 0;
  227. gbc.gridy = 3;
  228. gbc.insets = new Insets(5, 0, 0, 0);
  229. loginPanel.add(loginWarning, gbc);
  230. }
  231. private void buildNavigationPanel() {
  232. navigationPanel = new JPanel();
  233. navigationPanel.setLayout(new GridBagLayout());
  234. backBtn = new JButton("< Zurück");
  235. backBtn.addActionListener(new BackAction());
  236. chosenGame = new JLabel();
  237. border6 = new JPanel();
  238. border6.setOpaque(false);
  239. gbc.gridwidth = 1;
  240. gbc.weighty = 0.0;
  241. gbc.anchor = GridBagConstraints.CENTER;
  242. gbc.gridx = 0;
  243. gbc.gridy = 0;
  244. gbc.weightx = 0.0;
  245. gbc.insets = new Insets(5, 20, 5, 0);
  246. navigationPanel.add(backBtn, gbc);
  247. gbc.gridx = 1;
  248. gbc.gridy = 0;
  249. gbc.weightx = 1.0;
  250. gbc.insets = new Insets(0, 0, 0, 0);
  251. navigationPanel.add(border6, gbc);
  252. gbc.gridx = 2;
  253. gbc.gridy = 0;
  254. gbc.weightx = 0.0;
  255. gbc.insets = new Insets(5, 0, 5, 20);
  256. navigationPanel.add(chosenGame, gbc);
  257. }
  258. private void buildGamePanels() {
  259. gamePanel = new JPanel();
  260. gamePanel.setLayout(new BorderLayout());
  261. gamePanel.add(navigationPanel, BorderLayout.PAGE_START);
  262. //use of dummy panels because real panels have not been implemented yet
  263. fleetstormPanel = new JPanel();
  264. fleetstormPanel.setBackground(Color.BLUE);
  265. fourwinsPanel = new JPanel();
  266. fourwinsPanel.setBackground(Color.GREEN);
  267. tictactoePanel = new JPanel();
  268. tictactoePanel.setBackground(Color.YELLOW);
  269. leaderboardPanel = new JPanel();
  270. leaderboardPanel.setBackground(Color.RED);
  271. }
  272. private class LoginAction implements ActionListener {
  273. @Override
  274. public void actionPerformed(ActionEvent e) {
  275. String userInput = usernameInput.getText();
  276. if (!userInput.equals("")) {
  277. loginWarning.setText("");
  278. manager.loadPlayers(PLAYER_FILE);
  279. actualPlayer = manager.selectPlayer(userInput);
  280. buildExplorerPanel();
  281. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  282. frame.remove(loginPanel);
  283. frame.add(explorerPanel);
  284. frame.revalidate();
  285. frame.repaint();
  286. } else {
  287. loginWarning.setText("Kein Name eingegeben!");
  288. }
  289. }
  290. }
  291. private class GameAction implements ActionListener {
  292. @Override
  293. public void actionPerformed(ActionEvent e) {
  294. //each button adds a different dummy panel to the gamePanel
  295. if (e.getSource() == fleetstormBtn) {
  296. actualGame = Game.FLEETSTORM;
  297. chosenGame.setText("Schiffe versenken");
  298. gamePanel.add(fleetstormPanel, BorderLayout.CENTER);
  299. } else if (e.getSource() == fourwinsBtn) {
  300. actualGame = Game.FOURWINS;
  301. chosenGame.setText("Vier gewinnt");
  302. gamePanel.add(fourwinsPanel, BorderLayout.CENTER);
  303. } else if (e.getSource() == tictactoeBtn) {
  304. actualGame = Game.TICTACTOE;
  305. chosenGame.setText("TicTacToe");
  306. gamePanel.add(tictactoePanel, BorderLayout.CENTER);
  307. } else if (e.getSource() == leaderboardBtn) {
  308. actualGame = Game.LEADERBOARD;
  309. chosenGame.setText("Leaderboard");
  310. gamePanel.add(leaderboardPanel, BorderLayout.CENTER);
  311. }
  312. frame.remove(explorerPanel);
  313. frame.add(gamePanel);
  314. frame.revalidate();
  315. frame.repaint();
  316. }
  317. }
  318. private class LogoutAction implements ActionListener {
  319. @Override
  320. public void actionPerformed(ActionEvent e) {
  321. manager.savePlayers(PLAYER_FILE);
  322. buildLoginPanel();
  323. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  324. frame.remove(explorerPanel);
  325. frame.add(loginPanel);
  326. frame.revalidate();
  327. frame.repaint();
  328. }
  329. }
  330. private class ExitAction implements ActionListener {
  331. @Override
  332. public void actionPerformed(ActionEvent e) {
  333. manager.savePlayers(PLAYER_FILE);
  334. System.exit(0);
  335. }
  336. }
  337. private class BackAction implements ActionListener {
  338. @Override
  339. public void actionPerformed(ActionEvent e) {
  340. switch (actualGame) {
  341. case FLEETSTORM:
  342. gamePanel.remove(fleetstormPanel);
  343. break;
  344. case FOURWINS:
  345. gamePanel.remove(fourwinsPanel);
  346. break;
  347. case TICTACTOE:
  348. gamePanel.remove(tictactoePanel);
  349. break;
  350. case LEADERBOARD:
  351. gamePanel.remove(leaderboardPanel);
  352. }
  353. actualPlayerPoints.setText("" + actualPlayer.getPoints());
  354. frame.remove(gamePanel);
  355. frame.add(explorerPanel);
  356. frame.revalidate();
  357. frame.repaint();
  358. }
  359. }
  360. public static void main(String[] args) {
  361. new GameExplorer();
  362. }
  363. }