113 lines
2.9 KiB
Java
113 lines
2.9 KiB
Java
|
/*
|
||
|
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 <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
package de.itsblue.ConnectFour;
|
||
|
|
||
|
import javax.swing.JPanel;
|
||
|
|
||
|
import de.itsblue.ConnectFour.Plate.PlateType;
|
||
|
|
||
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
|
||
|
public class PlateContainer extends JPanel {
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
private static final long serialVersionUID = 1L;
|
||
|
|
||
|
/**
|
||
|
* holds the current contained plate object
|
||
|
*/
|
||
|
private Plate containedPlate = null;
|
||
|
|
||
|
/**
|
||
|
* Constructor
|
||
|
*/
|
||
|
PlateContainer() {
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function to insert a plate into the container
|
||
|
*
|
||
|
* @param plate the plate object to insert
|
||
|
* @return true if inserted, false if container is alread occupied
|
||
|
*/
|
||
|
public boolean insertPlate(Plate plate) {
|
||
|
if (this.containsPlate())
|
||
|
return false;
|
||
|
|
||
|
System.out.println("adding plate " + plate);
|
||
|
|
||
|
this.containedPlate = plate;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function to get the contained plate
|
||
|
*
|
||
|
* @return null or the contained plate
|
||
|
*/
|
||
|
public Plate getContainedPlate() {
|
||
|
return this.containedPlate;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function to check if the container is occupied
|
||
|
*
|
||
|
* @return true if occupied, false if not
|
||
|
*/
|
||
|
public boolean containsPlate() {
|
||
|
if (this.containedPlate != null)
|
||
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function to clear the container
|
||
|
*
|
||
|
* @return if it was occupied: contained plate, else: null
|
||
|
*/
|
||
|
public Plate removePlate() {
|
||
|
if (!this.containsPlate())
|
||
|
return null;
|
||
|
|
||
|
this.remove(this.getComponents()[0]);
|
||
|
Plate ret = this.containedPlate;
|
||
|
this.containedPlate = null;
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void paint(Graphics g) {
|
||
|
g.setColor(Color.lightGray);
|
||
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||
|
|
||
|
if (this.containsPlate()) {
|
||
|
g.setColor(this.containedPlate.getType() == PlateType.X ? Color.RED : Color.BLACK);
|
||
|
} else
|
||
|
g.setColor(Color.white);
|
||
|
|
||
|
g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
|
||
|
(int) (this.getHeight() * 0.8));
|
||
|
}
|
||
|
}
|