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

161 lines
4.4 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 de.itsblue.ConnectFour.Plate.PlateType;
import java.awt.*;
/**
* 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
*/
public class PlateContainer extends Canvas {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* holds the current contained plate object
*/
private Plate containedPlate = null;
/**
* is the container currently highlighted?
*/
private boolean highlighted = false;
private Image containerImg;
private Image plateRedImg;
private Image plateYellowImg;
private Image highlightImg;
/**
* Constructor
*/
PlateContainer() {
// initialize all images
containerImg = Toolkit.getDefaultToolkit().getImage(ConnectFour.class.getResource("res/container.png"));
plateRedImg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("res/plateRed.png"));
plateYellowImg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("res/plateYellow.png"));
highlightImg = Toolkit.getDefaultToolkit().getImage(getClass().getResource("res/highlight.png"));
}
/**
* 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;
// repaint
this.paint(this.getGraphics());
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, also removes the highlighting
*
* @return if it was occupied: contained plate, else: <code>null</code>
*/
public Plate removePlate() {
if (!this.containsPlate())
return null;
Plate ret = this.containedPlate;
this.containedPlate = null;
this.highlighted = false;
// repaint
this.paint(this.getGraphics());
return ret;
}
/**
* Function to highlight the container
*/
public void highlight() {
this.highlighted = true;
// repaint
this.paint(this.getGraphics());
}
/**
* Override the paint function to draw the shape of the plate
*/
@Override
public void paint(Graphics g1) {
if (g1 == null)
return;
Graphics2D g = (Graphics2D) g1;
// draw background
g.drawImage(containerImg, 0, 0, this.getWidth(), this.getHeight(), this);
// draw contained plate
if (this.containsPlate()) {
Image plateImage = this.getContainedPlate().getType().equals(PlateType.O) ? this.plateYellowImg
: this.plateRedImg;
g.drawImage(plateImage, -1, -1, this.getWidth() + 2, this.getHeight() + 2, this);
}
// draw Highlight
if (this.highlighted) {
g.drawImage(highlightImg, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}