fixed scaling issues with GameBoard and ButtonRow

This commit is contained in:
Dorian Zedler 2020-02-22 09:29:52 +01:00
parent afb0fa4865
commit acb0c067c7
2 changed files with 40 additions and 16 deletions

View file

@ -4,6 +4,7 @@ import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;
import java.io.Serializable;
public class ButtonRow extends JPanel{
@ -14,7 +15,7 @@ public class ButtonRow extends JPanel{
// configure the layout
this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.setLayout(new GridLayout(1,7));
for (int i = 0; i < this.buttoncount; i++) {
this.inputButtons[i] = new JButton();
@ -28,19 +29,24 @@ public class ButtonRow extends JPanel{
this.buttoncount = buttoncount;
this.inputButtons = new JButton[buttoncount];
this.InitButton();
System.out.println("test");
this.setBackground(Color.RED);
}
@Override
public void paint(Graphics g){
for (int i = 0; i < this.buttoncount; i++) {
Dimension size = this.getSize();
this.inputButtons[i].setPreferredSize(new Dimension(this.getWidth()/this.buttoncount, this.getHeight()));
for (int i = 0; i < this.buttoncount; i++) {
this.inputButtons[i].setPreferredSize(new Dimension(size.width/this.buttoncount, size.height));
}
super.paint(g);
}
@Override
public void setPreferredSize(Dimension preferredSize) {
super.setPreferredSize(preferredSize);
}
}

View file

@ -30,6 +30,10 @@ public class ConnectFour extends JFrame {
*/
private static final long serialVersionUID = 1L;
private GameBoard gameBoard;
private ButtonRow buttonRow;
ConnectFour() {
// Constructor
@ -42,26 +46,22 @@ public class ConnectFour extends JFrame {
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
// Board initilisieren
ButtonRow buttonRow = new ButtonRow(7);
buttonRow.setPreferredSize(new Dimension(400,20));
this.buttonRow = new ButtonRow(7);
this.gameBoard = new GameBoard();
c.gridy = 0;
add(buttonRow, c);
GameBoard board = new GameBoard();
board.setPreferredSize(new Dimension(400, 400));
c.gridy = 1;
this.add(board, c);
this.add(gameBoard, c);
// plate in das Board einfügen
board.insertPlate(new Plate(PlateType.O), 1);
gameBoard.insertPlate(new Plate(PlateType.O), 1);
// ein paar mehr plates einfügen
for (int i = 0; i < 7; i++) {
String res = board.insertPlate(new Plate(PlateType.X), 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));
@ -72,6 +72,24 @@ public class ConnectFour extends JFrame {
// finish up
this.pack();
this.setVisible(true);
}
public boolean landscape() {
return this.getSize().height < this.getSize().width;
}
@Override
public void validate() {
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;
this.gameBoard.setPreferredSize(boardSize);
this.buttonRow.setPreferredSize(
new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7));
super.validate();
}
public static void main(final String[] args) {