added some comments, forcing aspect ratio now

This commit is contained in:
Dorian Zedler 2020-02-16 19:46:57 +01:00
parent 6f4d3eb6ef
commit c3f5f04dbd
4 changed files with 80 additions and 50 deletions

View file

@ -31,13 +31,6 @@ public class ConnectFour extends JFrame {
*/
private static final long serialVersionUID = 1L;
@Override
public void setTitle(String name) {
super.setTitle(name);
}
ConnectFour() {
// Constructor
@ -50,7 +43,7 @@ public class ConnectFour extends JFrame {
this.getContentPane().setLayout(new GridBagLayout());
GameBoard board = new GameBoard();
//board.setPreferredSize(new Dimension(600,600));
board.setPreferredSize(new Dimension(200,200));
this.add(board);
for(int i = 0; i < 7; i++) {

View file

@ -28,8 +28,6 @@ public class GameBoard extends JPanel {
*/
private static final long serialVersionUID = 1L;
private boolean isInited = false;
/**
* The rows of the board
*/
@ -49,7 +47,6 @@ public class GameBoard extends JPanel {
* Constructor
*/
GameBoard() {
this.setPreferredSize(new Dimension(this.BoardColumns * 50, this.BoardRows * 50));
this.initBoard();
}
@ -108,35 +105,44 @@ public class GameBoard extends JPanel {
this.add(this.BoardContainers[j][i]);
}
}
this.isInited = true;
}
public int getSuggestedContainerSize(Dimension parentSize) {
int containerSize;
if (parentSize.getWidth() / this.BoardColumns > parentSize.getHeight() / this.BoardRows)
containerSize = (int) parentSize.getHeight() / this.BoardRows;
else
containerSize = (int) parentSize.getWidth() / this.BoardColumns;
return containerSize;
}
/**
* Override paint function to rescale all caontainers on every repaint
*/
@Override
public void paint(Graphics g) {
System.out.println("updating sizes");
int containerSize;
if (this.getSize().getWidth() / this.BoardColumns > this.getSize().getHeight() / this.BoardRows)
containerSize = (int) this.getSize().getHeight() / this.BoardRows;
else
containerSize = (int) this.getSize().getWidth() / this.BoardColumns;
containerSize = 30;
System.out.println(containerSize);
System.out.println(this.getSize());
for (PlateContainer[] plateContainers : BoardContainers) {
for (PlateContainer plateContainer : plateContainers) {
//plateContainer.setPreferredSize(new Dimension(containerSize, containerSize));
plateContainer.setPreferredSize(new Dimension(this.getSuggestedContainerSize(this.getSize()),
this.getSuggestedContainerSize(this.getSize())));
}
}
super.paint(g);
}
@Override
public void setPreferredSize(Dimension preferredSize) {
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.BoardColumns,
this.getSuggestedContainerSize(preferredSize) * this.BoardRows);
super.setPreferredSize(newSize);
}
/*
* void winningCondition() { for (int i = 0; i <= 2; i++) for (int j = 0; j <=
* 6; j++) { if (GameBoard[i][j] == p && GameBoard[i + 1][j] == p && GameBoard[i

View file

@ -17,17 +17,20 @@
*/
package de.itsblue.ConnectFour;
import javax.swing.*;
import java.awt.*;
public class Plate {
/**
* Plate models to plate or 'coin' to be inserted into
* game grid. It therefore has a type (X or O) which
* comes with a color (X = RED; O = BLACK) and a value
* (X = -1; O = 1) for further processing. this is a
* helper class and does not come with any standalone
* usage.
*
* @author Oliver Schappacher
* @author Dorian Zedler
*/
private static final long serialVersionUID = 1L;
public class Plate {
/**
* Enum containing all plate types
@ -43,14 +46,16 @@ public class Plate {
/**
* Constructor
*
* @param type type of the plate
*/
Plate(PlateType type) {
this.setType(type);
this.type = type;
}
/**
* Function to get the type of the plate
*
* @return PlateType
*/
public PlateType getType() {
@ -58,12 +63,35 @@ public class Plate {
}
/**
* Function to change the type of the plate
* Function to get the type of the plate as int
*
* @param type the type to set the plate to
* @return plate type as int (O=1; X=-1)
*/
private void setType(PlateType type) {
this.type = type;
public int getValue() {
switch (this.type) {
case O:
return 1;
case X:
return -1;
default:
return 0;
}
}
/**
* Function to get the color of the plate
*
* @return the color of the plate
*/
public Color getColor() {
switch (this.type) {
case O:
return Color.BLACK;
case X:
return Color.RED;
default:
return Color.WHITE;
}
}
}

View file

@ -19,10 +19,6 @@
package de.itsblue.ConnectFour;
import javax.swing.JPanel;
import de.itsblue.ConnectFour.Plate.PlateType;
import javax.swing.*;
import java.awt.*;
public class PlateContainer extends JPanel {
@ -72,7 +68,8 @@ public class PlateContainer extends JPanel {
/**
* Function to check if the container is occupied
*
* @return true if occupied, false if not
* @return <code>true</code> if occupied
* <code>false</code> if not
*/
public boolean containsPlate() {
if (this.containedPlate != null)
@ -84,7 +81,7 @@ public class PlateContainer extends JPanel {
/**
* Function to clear the container
*
* @return if it was occupied: contained plate, else: null
* @return if it was occupied: contained plate, else: <code>null</code>
*/
public Plate removePlate() {
if (!this.containsPlate())
@ -97,16 +94,22 @@ public class PlateContainer extends JPanel {
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());
if (this.containsPlate()) {
g.setColor(this.containedPlate.getType() == PlateType.X ? Color.RED : Color.BLACK);
} else
// 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));
}