added new controllbuttons, still no functionality

This commit is contained in:
oliver 2020-02-25 10:44:24 +01:00
parent b36b0f549f
commit 8ae82e9f9a
2 changed files with 127 additions and 23 deletions

View file

@ -37,6 +37,8 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
private ButtonRow buttonRow; private ButtonRow buttonRow;
private ControllRow controllRow;
private int player = 0; private int player = 0;
private Player players[] = new Player[2]; private Player players[] = new Player[2];
@ -44,9 +46,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
private GameType gameType; private GameType gameType;
enum GameType { enum GameType {
Local, Local, RemoteServer, RemoteClient
RemoteServer,
RemoteClient
} }
/** /**
@ -69,10 +69,19 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
// initialize ButtonRow // initialize ButtonRow
this.buttonRow = new ButtonRow(this.gameBoard.getColumns()); this.buttonRow = new ButtonRow(this.gameBoard.getColumns());
// initialize ContollRow
this.controllRow = new ControllRow(this.gameBoard.getColumns());
// add components to window // add components to window
c.gridx = 0;
c.gridy = 0; c.gridy = 0;
add(buttonRow, c); add(buttonRow, c);
c.gridx = 13;
c.gridy = 1;
add(controllRow, c);
c.gridx = 0;
c.gridy = 1; c.gridy = 1;
this.add(gameBoard, c); this.add(gameBoard, c);
@ -129,8 +138,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
if (player == 0) { if (player == 0) {
player = 1; player = 1;
} } else {
else{
player = 0; player = 0;
} }
@ -152,7 +160,7 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
*/ */
@Override @Override
public void movePerformed(int column, Player src) { 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; String res;
res = this.gameBoard.insertPlate(new Plate(src.usingPlateType), column); res = this.gameBoard.insertPlate(new Plate(src.usingPlateType), column);
@ -165,8 +173,8 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
System.out.println("A player won: " + winnerType); System.out.println("A player won: " + winnerType);
} }
if(this.gameType != GameType.RemoteClient) if (this.gameType != GameType.RemoteClient)
switchPlayer(); switchPlayer();
} }
} }
@ -186,6 +194,9 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
// set button row size // set button row size
this.buttonRow.setPreferredSize( this.buttonRow.setPreferredSize(
new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7)); 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 // call super
super.validate(); super.validate();
@ -198,13 +209,13 @@ public class ConnectFour extends JFrame implements PlayerMoveListener {
System.out.println("argc: " + args.length); System.out.println("argc: " + args.length);
System.out.println("args[0]: " + args[0]); System.out.println("args[0]: " + args[0]);
if(args.length <= 0) if (args.length <= 0)
game.startNewGame(GameType.Local); game.startNewGame(GameType.Local);
else if(args[0].equals("server")) else if (args[0].equals("server"))
game.startNewGame(GameType.RemoteServer); game.startNewGame(GameType.RemoteServer);
else if(args[0].equals("client")) else if (args[0].equals("client"))
game.startNewGame(GameType.RemoteClient); game.startNewGame(GameType.RemoteClient);
else else
System.out.println("Usage: java ConnectFour.java [server|client]"); System.out.println("Usage: java ConnectFour.java [server|client]");
} }
} }

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}