- added dynamic scaling for GameBoard and ButtonRow

- added handling for button press in ButtonRow
This commit is contained in:
Dorian Zedler 2020-02-22 10:40:38 +01:00
parent 27f4b72fe2
commit abd2cf6e86

View file

@ -19,12 +19,12 @@
package de.itsblue.ConnectFour;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import de.itsblue.ConnectFour.GameBoard;
import de.itsblue.ConnectFour.Plate.PlateType;
public class ConnectFour extends JFrame {
public class ConnectFour extends JFrame implements ActionListener {
/**
*
*/
@ -34,64 +34,102 @@ public class ConnectFour extends JFrame {
private ButtonRow buttonRow;
/**
* Constructor
*/
ConnectFour() {
// Constructor
// initialize window
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Connect 4");
this.setPreferredSize(new Dimension(600, 600));
// initialize layout
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Board initilisieren
this.buttonRow = new ButtonRow(7);
// initialize GameBoard
this.gameBoard = new GameBoard();
// initialize ButtonRow
this.buttonRow = new ButtonRow(this.gameBoard.getColumns());
buttonRow.addActionListenerToButtons(this);
// add components to window
c.gridy = 0;
add(buttonRow, c);
c.gridy = 1;
this.add(gameBoard, c);
// plate in das Board einfügen
gameBoard.insertPlate(new Plate(PlateType.O), 1);
// ein paar mehr plates einfügen
for (int i = 0; i < 7; i++) {
String res = gameBoard.insertPlate(new Plate(PlateType.X), i);
// wen das Rückgabewert weder "ok" noch "err" ist, hat jemand gewonnen
if (res != "ok" && res != "err")
System.out.println(PlateType.valueOf(res));
}
// board kann mit board.clearBoard(); geleert werden.
// finish up
this.pack();
this.setVisible(true);
}
/**
* Function to handle the next plate insertion
*
* @param column The column to insert the plate into
*/
private void insertNextPlate(int column) {
// TODO: change player everytime
String res = this.gameBoard.insertPlate(new Plate(PlateType.X), column);
if (res == "err") {
// beep in case of error
Toolkit.getDefaultToolkit().beep();
} else if (res != "ok" && res != "err") {
PlateType winnerType = PlateType.valueOf(res);
System.out.println("A player won: " + winnerType);
}
}
/**
* Function to check if the window is in landscape or portrait mode
*
* @return <code>true</code> if window is in landscape mode, <code>false</code>
* if it is not
*/
public boolean landscape() {
return this.getSize().height < this.getSize().width;
}
/**
* Override validate in order to resacle the components when the window is
* rescaled
*/
@Override
public void validate() {
// determine optimal size for board
Dimension boardSize = new Dimension();
boardSize.width = this.landscape() ? this.getSize().height / 2 : this.getSize().width / 2;
boardSize.height = this.landscape() ? this.getSize().width : this.getSize().height;
// set board size
this.gameBoard.setPreferredSize(boardSize);
// set button row size
this.buttonRow.setPreferredSize(
new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7));
// call super
super.validate();
}
/**
* Catch the actions from the button row
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof PlateInsertButton) {
// if the action source was a PlateInsertButton
// -> trigger a plate insertion
PlateInsertButton source = (PlateInsertButton) e.getSource();
this.insertNextPlate(source.getIndex());
}
}
public static void main(final String[] args) {
System.out.println(new ConnectFour());
}