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
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;
2020-02-25 20:07:11 +01:00
import de.itsblue.ConnectFour.Plate.PlateType;
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-25 20:07:11 +01:00
public class PlateContainer extends Canvas {
/**
*
*/
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-25 20:07:11 +01:00
private Image containerImg;
private Image plateRedImg;
private Image plateYellowImg;
private Image highlightImg;
/**
* Constructor
*/
PlateContainer() {
2020-02-25 20:07:11 +01:00
// initialize all images
2020-02-25 21:19:29 +01:00
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;
2020-02-22 10:41:16 +01:00
// 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
*
2020-02-17 16:55:21 +01:00
* @return <code>true</code> if occupied <code>false</code> if not
*/
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
*
* @return if it was occupied: contained plate, else: <code>null</code>
*/
public Plate removePlate() {
if (!this.containsPlate())
return null;
2020-02-17 16:55:21 +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());
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
}
/**
* Override the paint function to draw the shape of the plate
*/
@Override
2020-02-25 20:07:11 +01:00
public void paint(Graphics g1) {
if (g1 == null)
2020-02-22 10:41:16 +01:00
return;
2020-02-25 20:07:11 +01:00
Graphics2D g = (Graphics2D) g1;
2020-02-25 20:07:11 +01:00
// draw background
g.drawImage(containerImg, 0, 0, this.getWidth(), this.getHeight(), this);
2020-02-25 20:07:11 +01:00
// 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);
}
2020-02-17 16:55:21 +01:00
2020-02-25 20:07:11 +01:00
// draw Highlight
2020-02-17 16:55:21 +01:00
if (this.highlighted) {
2020-02-25 20:07:11 +01:00
g.drawImage(highlightImg, 0, 0, this.getWidth(), this.getHeight(), this);
2020-02-17 16:55:21 +01:00
}
}
2020-02-25 20:07:11 +01:00
}