From 27f4b72fe2422fe183194881db83a958a9c00c55 Mon Sep 17 00:00:00 2001 From: dorian Date: Sat, 22 Feb 2020 10:39:57 +0100 Subject: [PATCH] Fixed layout issues --- src/de/itsblue/ConnectFour/ButtonRow.java | 102 +++++++++++++++------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/src/de/itsblue/ConnectFour/ButtonRow.java b/src/de/itsblue/ConnectFour/ButtonRow.java index 9cf1f05..dce7ec3 100644 --- a/src/de/itsblue/ConnectFour/ButtonRow.java +++ b/src/de/itsblue/ConnectFour/ButtonRow.java @@ -1,52 +1,94 @@ -package de.itsblue.ConnectFour; +/* + Connect four - written in java + Copyright (C) 2020 Oliver Schappacher and Dorian Zedler -import javax.swing.JButton; + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package de.itsblue.ConnectFour; import javax.swing.*; import java.awt.*; -import java.io.Serializable; +import java.awt.event.*; -public class ButtonRow extends JPanel{ +/** + * Button row is a class meant for usage with de.itsblue.ConnectFour. It is a + * row contining a given umber of buttons. + * + * @author Oliver Schappacher + * @author Dorian Zedler + */ +public class ButtonRow extends JPanel { - JButton inputButtons[]; - int buttoncount = 0; + /** + * + */ + private static final long serialVersionUID = 1L; - public void InitButton() { + /** + * Array that sores all buttons in the row + */ + private PlateInsertButton inputButtons[]; + + /** + * The number of buttons in the row + */ + private int buttonCount = 0; + + /** + * Constructor + * + * @param buttonCount The number of buttons to create + */ + ButtonRow(int buttonCount) { + // configure basic values + this.buttonCount = buttonCount; + this.inputButtons = new PlateInsertButton[buttonCount]; - // configure the layout - this.setLayout(new GridLayout(1,7)); + this.setLayout(new GridLayout(1, 7)); - for (int i = 0; i < this.buttoncount; i++) { - this.inputButtons[i] = new JButton(); - this.inputButtons[i].setText(""+i); + // insert all the buttons + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i] = new PlateInsertButton(i); + this.inputButtons[i].setText("" + i); this.add(this.inputButtons[i]); } - - } - - ButtonRow(int buttoncount) { - this.buttoncount = buttoncount; - this.inputButtons = new JButton[buttoncount]; - this.InitButton(); - this.setBackground(Color.RED); } + /** + * Function to add an ActionListener to all contained buttons + * + * @param listener + */ + public void addActionListenerToButtons(ActionListener listener) { + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i].addActionListener(listener); + } + } + + /** + * Override the paint function in order to adjust the button size when rescaling + */ @Override - public void paint(Graphics g){ + public void paint(Graphics g) { Dimension size = this.getSize(); - for (int i = 0; i < this.buttoncount; i++) { - this.inputButtons[i].setPreferredSize(new Dimension(size.width/this.buttoncount, size.height)); + 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); - } - - }