2020-02-14 19:21:49 +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.
|
|
|
|
|
|
|
|
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.*;
|
|
|
|
|
2020-02-17 16:55:21 +01:00
|
|
|
/**
|
|
|
|
* PlateContainer is a visual element intendet to be used in a GameBoard in
|
|
|
|
* order to store a plate and indicate what kind of plate it contains. It can ge
|
|
|
|
* highlighted for examplte to indicate that it was involeved in the end of a
|
|
|
|
* game.
|
|
|
|
*
|
|
|
|
* @author Dorian Zedler
|
|
|
|
*/
|
2020-02-14 19:21:49 +01:00
|
|
|
public class PlateContainer extends JPanel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds the current contained plate object
|
|
|
|
*/
|
|
|
|
private Plate containedPlate = null;
|
|
|
|
|
2020-02-17 16:55:21 +01:00
|
|
|
/**
|
|
|
|
* is the container currently highlighted?
|
|
|
|
*/
|
|
|
|
private boolean highlighted = false;
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
this.containedPlate = plate;
|
|
|
|
|
2020-02-22 10:41:16 +01:00
|
|
|
// repaint
|
|
|
|
this.paint(this.getGraphics());
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
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
|
|
|
|
*
|
2020-02-17 16:55:21 +01:00
|
|
|
* @return <code>true</code> if occupied <code>false</code> if not
|
2020-02-14 19:21:49 +01:00
|
|
|
*/
|
|
|
|
public boolean containsPlate() {
|
|
|
|
if (this.containedPlate != null)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-17 16:55:21 +01:00
|
|
|
* Function to clear the container, also removes the highlighting
|
2020-02-14 19:21:49 +01:00
|
|
|
*
|
2020-02-16 19:46:57 +01:00
|
|
|
* @return if it was occupied: contained plate, else: <code>null</code>
|
2020-02-14 19:21:49 +01:00
|
|
|
*/
|
|
|
|
public Plate removePlate() {
|
|
|
|
if (!this.containsPlate())
|
|
|
|
return null;
|
2020-02-17 16:55:21 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
Plate ret = this.containedPlate;
|
|
|
|
this.containedPlate = null;
|
|
|
|
|
2020-02-17 16:55:21 +01:00
|
|
|
this.highlighted = false;
|
|
|
|
|
2020-02-22 10:41:16 +01:00
|
|
|
// repaint
|
|
|
|
this.paint(this.getGraphics());
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:55:21 +01:00
|
|
|
/**
|
|
|
|
* Function to highlight the container
|
|
|
|
*/
|
|
|
|
public void highlight() {
|
|
|
|
this.highlighted = true;
|
2020-02-22 10:41:16 +01:00
|
|
|
|
|
|
|
// repaint
|
|
|
|
this.paint(this.getGraphics());
|
2020-02-17 16:55:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
/**
|
|
|
|
* Override the paint function to draw the shape of the plate
|
|
|
|
*/
|
2020-02-14 19:21:49 +01:00
|
|
|
@Override
|
|
|
|
public void paint(Graphics g) {
|
2020-02-22 10:41:16 +01:00
|
|
|
if(g == null)
|
|
|
|
return;
|
2020-02-16 19:46:57 +01:00
|
|
|
// draw background
|
2020-02-14 19:21:49 +01:00
|
|
|
g.setColor(Color.lightGray);
|
|
|
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
// set color according to contained plate
|
|
|
|
if (this.containsPlate())
|
|
|
|
g.setColor(this.containedPlate.getColor());
|
|
|
|
else
|
2020-02-14 19:21:49 +01:00
|
|
|
g.setColor(Color.white);
|
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
// draw plate
|
2020-02-14 19:21:49 +01:00
|
|
|
g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
|
|
|
|
(int) (this.getHeight() * 0.8));
|
2020-02-17 16:55:21 +01:00
|
|
|
|
|
|
|
if (this.highlighted) {
|
|
|
|
g.setColor(Color.green);
|
|
|
|
Graphics2D g2 = (Graphics2D) g;
|
|
|
|
g2.setStroke(new BasicStroke((int) (this.getWidth() * 0.1)));
|
|
|
|
|
|
|
|
g2.drawOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
|
|
|
|
(int) (this.getHeight() * 0.8));
|
|
|
|
}
|
2020-02-14 19:21:49 +01:00
|
|
|
}
|
|
|
|
}
|