This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
connect-four/src/de/itsblue/ConnectFour/ButtonRow.java

126 lines
3.4 KiB
Java
Raw Normal View History

2020-02-22 10:39:57 +01:00
/*
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.
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
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 <http://www.gnu.org/licenses/>.
*/
package de.itsblue.ConnectFour;
2020-02-21 15:48:56 +01:00
import javax.swing.*;
import java.awt.*;
2020-02-22 10:39:57 +01:00
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;
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
/**
* Array that sores all buttons in the row
*/
private PlateInsertButton inputButtons[];
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
/**
* The number of buttons in the row
*/
private int buttonCount = 0;
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
/**
* Constructor
*
* @param buttonCount The number of buttons to create
*/
ButtonRow(int buttonCount) {
// configure basic values
this.buttonCount = buttonCount;
this.inputButtons = new PlateInsertButton[buttonCount];
2020-02-21 15:48:56 +01:00
// configure the layout
2020-02-22 10:39:57 +01:00
this.setLayout(new GridLayout(1, 7));
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
// insert all the buttons
for (int i = 0; i < this.buttonCount; i++) {
this.inputButtons[i] = new PlateInsertButton(i);
this.inputButtons[i].setText("" + i);
2020-02-21 15:48:56 +01:00
this.add(this.inputButtons[i]);
}
}
2020-02-22 10:39:57 +01:00
/**
* 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);
}
2020-02-21 15:48:56 +01:00
}
/**
* 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);
}
}
2020-02-22 10:39:57 +01:00
/**
* Override the paint function in order to adjust the button size when rescaling
*/
2020-02-21 15:48:56 +01:00
@Override
2020-02-22 10:39:57 +01:00
public void paint(Graphics g) {
Dimension size = this.getSize();
2020-02-21 15:48:56 +01:00
2020-02-22 10:39:57 +01:00
for (int i = 0; i < this.buttonCount; i++) {
this.inputButtons[i].setPreferredSize(new Dimension(size.width / this.buttonCount, size.height));
2020-02-21 15:48:56 +01:00
}
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);
}
}
2020-02-21 15:48:56 +01:00
}