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/PlateContainer.java

115 lines
3 KiB
Java
Raw Normal View History

/*
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 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 <code>true</code> if occupied
* <code>false</code> 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: <code>null</code>
*/
public Plate removePlate() {
if (!this.containsPlate())
return null;
2020-02-17 08:38:16 +01:00
Plate ret = this.containedPlate;
this.containedPlate = null;
return ret;
}
/**
* Override the paint function to draw the shape of the plate
*/
@Override
public void paint(Graphics g) {
// draw background
g.setColor(Color.lightGray);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
// set color according to contained plate
if (this.containsPlate())
g.setColor(this.containedPlate.getColor());
else
g.setColor(Color.white);
// draw plate
g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
(int) (this.getHeight() * 0.8));
}
}