From acb0c067c7e449ef04d9067a43540a33ad5e6966 Mon Sep 17 00:00:00 2001 From: dorian Date: Sat, 22 Feb 2020 09:29:52 +0100 Subject: [PATCH] fixed scaling issues with GameBoard and ButtonRow --- src/de/itsblue/ConnectFour/ButtonRow.java | 14 +++++-- src/de/itsblue/ConnectFour/ConnectFour.java | 42 +++++++++++++++------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/de/itsblue/ConnectFour/ButtonRow.java b/src/de/itsblue/ConnectFour/ButtonRow.java index 0305617..9cf1f05 100644 --- a/src/de/itsblue/ConnectFour/ButtonRow.java +++ b/src/de/itsblue/ConnectFour/ButtonRow.java @@ -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); + } } diff --git a/src/de/itsblue/ConnectFour/ConnectFour.java b/src/de/itsblue/ConnectFour/ConnectFour.java index 891247e..9dc156d 100644 --- a/src/de/itsblue/ConnectFour/ConnectFour.java +++ b/src/de/itsblue/ConnectFour/ConnectFour.java @@ -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)); - add(buttonRow,c); + 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) {