/* 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 . */ package de.itsblue.ConnectFour; import javax.swing.JPanel; 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 JPanel { /** * */ 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; /** * 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; 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, also removes the highlighting * * @return if it was occupied: contained plate, else: null */ public Plate removePlate() { if (!this.containsPlate()) return null; Plate ret = this.containedPlate; this.containedPlate = null; this.highlighted = false; return ret; } /** * Function to highlight the container */ public void highlight() { this.highlighted = true; } /** * 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)); 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)); } } }