diff --git a/src/de/itsblue/ConnectFour/ConnectFour.java b/src/de/itsblue/ConnectFour/ConnectFour.java index 10dc4d5..706fdc8 100644 --- a/src/de/itsblue/ConnectFour/ConnectFour.java +++ b/src/de/itsblue/ConnectFour/ConnectFour.java @@ -37,6 +37,8 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { private ButtonRow buttonRow; + private ControllRow controllRow; + private int player = 0; private Player players[] = new Player[2]; @@ -44,9 +46,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { private GameType gameType; enum GameType { - Local, - RemoteServer, - RemoteClient + Local, RemoteServer, RemoteClient } /** @@ -69,10 +69,19 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { // initialize ButtonRow this.buttonRow = new ButtonRow(this.gameBoard.getColumns()); + // initialize ContollRow + this.controllRow = new ControllRow(this.gameBoard.getColumns()); + // add components to window + c.gridx = 0; c.gridy = 0; add(buttonRow, c); + c.gridx = 13; + c.gridy = 1; + add(controllRow, c); + + c.gridx = 0; c.gridy = 1; this.add(gameBoard, c); @@ -89,7 +98,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { this.players[0].addMoveListener(this); this.players[1] = new LocalPlayer(this.buttonRow, PlateType.X); this.players[1].addMoveListener(this); - + this.player = 0; this.players[player].setIsMyTurn(true); break; @@ -98,10 +107,10 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { case RemoteClient: { this.players[0] = new LocalPlayer(this.buttonRow, null); this.players[1] = new RemotePlayerClient(this.buttonRow, "localhost", this.players[0]); - + this.players[0].addMoveListener(this); this.players[1].addMoveListener(this); - + break; } @@ -111,7 +120,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { this.players[0].addMoveListener(this); this.players[1].addMoveListener(this); - + this.player = 0; this.players[player].setIsMyTurn(true); this.players[1].setIsMyTurn(false); @@ -129,8 +138,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { if (player == 0) { player = 1; - } - else{ + } else { player = 0; } @@ -152,11 +160,11 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { */ @Override public void movePerformed(int column, Player src) { - if(this.players[this.player].equals(src) || this.gameType == GameType.RemoteClient) { + if (this.players[this.player].equals(src) || this.gameType == GameType.RemoteClient) { String res; - res = this.gameBoard.insertPlate(new Plate(src.usingPlateType), column); - + res = this.gameBoard.insertPlate(new Plate(src.usingPlateType), column); + if (res == "err") { // beep in case of error Toolkit.getDefaultToolkit().beep(); @@ -164,9 +172,9 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { PlateType winnerType = PlateType.valueOf(res); System.out.println("A player won: " + winnerType); } - - if(this.gameType != GameType.RemoteClient) - switchPlayer(); + + if (this.gameType != GameType.RemoteClient) + switchPlayer(); } } @@ -186,6 +194,9 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { // set button row size this.buttonRow.setPreferredSize( new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7)); + // set button column size + this.controllRow.setPreferredSize( + new Dimension(this.gameBoard.getPreferredSize().width / 4, this.gameBoard.getPreferredSize().height)); // call super super.validate(); @@ -198,13 +209,13 @@ public class ConnectFour extends JFrame implements PlayerMoveListener { System.out.println("argc: " + args.length); System.out.println("args[0]: " + args[0]); - if(args.length <= 0) - game.startNewGame(GameType.Local); - else if(args[0].equals("server")) - game.startNewGame(GameType.RemoteServer); - else if(args[0].equals("client")) - game.startNewGame(GameType.RemoteClient); - else - System.out.println("Usage: java ConnectFour.java [server|client]"); + if (args.length <= 0) + game.startNewGame(GameType.Local); + else if (args[0].equals("server")) + game.startNewGame(GameType.RemoteServer); + else if (args[0].equals("client")) + game.startNewGame(GameType.RemoteClient); + else + System.out.println("Usage: java ConnectFour.java [server|client]"); } } \ No newline at end of file diff --git a/src/de/itsblue/ConnectFour/ControllRow.java b/src/de/itsblue/ConnectFour/ControllRow.java new file mode 100644 index 0000000..69acbf8 --- /dev/null +++ b/src/de/itsblue/ConnectFour/ControllRow.java @@ -0,0 +1,93 @@ +/* + Connect four - written in java + Copyright (C) 2020 Oliver Schappacher and Dorian Zedler + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package de.itsblue.ConnectFour; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +/** + * Button row is a class meant for usage with de.itsblue.ConnectFour. + * + * @author Oliver Schappacher + * @author Dorian Zedler + */ +public class ControllRow extends JPanel { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Array that sores all buttons in the column + */ + private JButton inputButtons[]; + + /** + * The number of buttons in the row + */ + private int buttonCount = 0; + + /** + * Constructor + * + * @param buttonCount The number of buttons to create + */ + ControllRow(int buttonCount) { + // configure basic values + this.buttonCount = 3; + this.inputButtons = new PlateInsertButton[buttonCount]; + String[] Titel = {"exit", "new Game", "current Player"}; + + // configure the layout + this.setLayout(new GridLayout(7, 1)); + + // insert all the buttons + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i] = new PlateInsertButton(i); + this.inputButtons[i].setText(Titel[i]); + this.add(this.inputButtons[i]); + } + } + /** + * Function to add an ActionListener to all contained buttons + * + * @param listener + */ + public void addActionListenerToButtons(ActionListener listener) { + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i].addActionListener(listener); + } + } + + /** + * Override the paint function in order to adjust the button size when rescaling + */ + @Override + public void paint(Graphics g) { + Dimension size = this.getSize(); + + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i].setPreferredSize(new Dimension(size.width / this.buttonCount, size.height)); + } + + super.paint(g); + } +}