fixed scaling issues with GameBoard and ButtonRow
This commit is contained in:
parent
afb0fa4865
commit
acb0c067c7
2 changed files with 40 additions and 16 deletions
|
@ -4,6 +4,7 @@ import javax.swing.JButton;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
public class ButtonRow extends JPanel{
|
public class ButtonRow extends JPanel{
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ public class ButtonRow extends JPanel{
|
||||||
|
|
||||||
|
|
||||||
// configure the layout
|
// 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++) {
|
for (int i = 0; i < this.buttoncount; i++) {
|
||||||
this.inputButtons[i] = new JButton();
|
this.inputButtons[i] = new JButton();
|
||||||
|
@ -28,19 +29,24 @@ public class ButtonRow extends JPanel{
|
||||||
this.buttoncount = buttoncount;
|
this.buttoncount = buttoncount;
|
||||||
this.inputButtons = new JButton[buttoncount];
|
this.inputButtons = new JButton[buttoncount];
|
||||||
this.InitButton();
|
this.InitButton();
|
||||||
System.out.println("test");
|
this.setBackground(Color.RED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paint(Graphics g){
|
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);
|
super.paint(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPreferredSize(Dimension preferredSize) {
|
||||||
|
super.setPreferredSize(preferredSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ public class ConnectFour extends JFrame {
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private GameBoard gameBoard;
|
||||||
|
|
||||||
|
private ButtonRow buttonRow;
|
||||||
|
|
||||||
ConnectFour() {
|
ConnectFour() {
|
||||||
// Constructor
|
// Constructor
|
||||||
|
|
||||||
|
@ -42,26 +46,22 @@ public class ConnectFour extends JFrame {
|
||||||
this.getContentPane().setLayout(new GridBagLayout());
|
this.getContentPane().setLayout(new GridBagLayout());
|
||||||
GridBagConstraints c = new GridBagConstraints();
|
GridBagConstraints c = new GridBagConstraints();
|
||||||
|
|
||||||
c.gridy = 0;
|
|
||||||
|
|
||||||
// Board initilisieren
|
// Board initilisieren
|
||||||
ButtonRow buttonRow = new ButtonRow(7);
|
this.buttonRow = new ButtonRow(7);
|
||||||
buttonRow.setPreferredSize(new Dimension(400,20));
|
this.gameBoard = new GameBoard();
|
||||||
add(buttonRow,c);
|
|
||||||
|
|
||||||
GameBoard board = new GameBoard();
|
c.gridy = 0;
|
||||||
|
add(buttonRow, c);
|
||||||
|
|
||||||
board.setPreferredSize(new Dimension(400, 400));
|
|
||||||
c.gridy = 1;
|
c.gridy = 1;
|
||||||
this.add(board, c);
|
this.add(gameBoard, c);
|
||||||
|
|
||||||
|
|
||||||
// plate in das Board einfügen
|
// 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
|
// ein paar mehr plates einfügen
|
||||||
for (int i = 0; i < 7; i++) {
|
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
|
// wen das Rückgabewert weder "ok" noch "err" ist, hat jemand gewonnen
|
||||||
if (res != "ok" && res != "err")
|
if (res != "ok" && res != "err")
|
||||||
System.out.println(PlateType.valueOf(res));
|
System.out.println(PlateType.valueOf(res));
|
||||||
|
@ -72,6 +72,24 @@ public class ConnectFour extends JFrame {
|
||||||
// finish up
|
// finish up
|
||||||
this.pack();
|
this.pack();
|
||||||
this.setVisible(true);
|
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) {
|
public static void main(final String[] args) {
|
||||||
|
|
Reference in a new issue