added Buttons

This commit is contained in:
oliver 2020-02-21 15:48:56 +01:00
parent f4098bf044
commit b3a02801db
3 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,46 @@
package de.itsblue.ConnectFour;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;
public class ButtonRow extends JPanel{
JButton inputButtons[];
int buttoncount = 0;
public void InitButton() {
// configure the layout
this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
for (int i = 0; i < this.buttoncount; i++) {
this.inputButtons[i] = new JButton();
this.inputButtons[i].setText(""+i);
this.add(this.inputButtons[i]);
}
}
ButtonRow(int buttoncount) {
this.buttoncount = buttoncount;
this.inputButtons = new JButton[buttoncount];
this.InitButton();
System.out.println("test");
}
@Override
public void paint(Graphics g){
for (int i = 0; i < this.buttoncount; i++) {
this.inputButtons[i].setPreferredSize(new Dimension(this.getWidth()/this.buttoncount, this.getHeight()));
}
super.paint(g);
}
}

View file

@ -40,11 +40,21 @@ public class ConnectFour extends JFrame {
this.setPreferredSize(new Dimension(600, 600)); this.setPreferredSize(new Dimension(600, 600));
this.getContentPane().setLayout(new GridBagLayout()); this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
// Board initilisieren // Board initilisieren
ButtonRow buttonRow = new ButtonRow(7);
buttonRow.setPreferredSize(new Dimension(400,20));
add(buttonRow,c);
GameBoard board = new GameBoard(); GameBoard board = new GameBoard();
board.setPreferredSize(new Dimension(400, 400)); board.setPreferredSize(new Dimension(400, 400));
this.add(board); c.gridy = 1;
this.add(board, c);
// plate in das Board einfügen // plate in das Board einfügen
board.insertPlate(new Plate(PlateType.O), 1); board.insertPlate(new Plate(PlateType.O), 1);

View file

@ -23,6 +23,7 @@ import java.awt.*;
import de.itsblue.ConnectFour.Plate.PlateType; import de.itsblue.ConnectFour.Plate.PlateType;
/** /**
* GameBoard is a fully usable connect4 game board. It can take plates and * GameBoard is a fully usable connect4 game board. It can take plates and
* insert them into a given column and check if somebody won the game. * insert them into a given column and check if somebody won the game.
@ -63,8 +64,10 @@ public class GameBoard extends JPanel {
*/ */
GameBoard() { GameBoard() {
this.initBoard(); this.initBoard();
} }
/** /**
* Function to insert a plate into a specific column * Function to insert a plate into a specific column
* *
@ -116,6 +119,8 @@ public class GameBoard extends JPanel {
* Function to fill the board with containers * Function to fill the board with containers
*/ */
private void initBoard() { private void initBoard() {
// configure the main layout // configure the main layout
this.setLayout(new GridLayout(this.BoardRows, this.BoardColumns)); this.setLayout(new GridLayout(this.BoardRows, this.BoardColumns));
@ -129,8 +134,12 @@ public class GameBoard extends JPanel {
this.add(this.BoardContainers[j][i]); this.add(this.BoardContainers[j][i]);
} }
} }
} }
/** /**
* Function to calculate a size for the containers to fit a given dimension * Function to calculate a size for the containers to fit a given dimension
* *