/* Connect four - written in java Copyright (C) 2020 Oliver Schappacher and Dorian Zedler 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.awt.event.*; /** * 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 { /** * */ private static final long serialVersionUID = 1L; /** * 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)); // 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]); } } /** * 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); } } /** * Function to remove an ActionListener from all contained buttons * * @param listener */ public void removeActionListemerFromButtons(ActionListener listener) { for (int i = 0; i < this.buttonCount; i++) { this.inputButtons[i].removeActionListener(listener); } } /** * Function to change the color of all buttons * * @param color color to change to */ public void setColor(Color color) { for (PlateInsertButton plateInsertButton : inputButtons) { plateInsertButton.setBackground(color); } } /** * Override the paint function in order to adjust the button size when rescaling */ @Override 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)); } super.paint(g); } @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); for (int i = 0; i < this.buttonCount; i++) { this.inputButtons[i].setEnabled(enabled); } } }