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

@ -29,15 +29,8 @@ public class ConnectFour extends JFrame {
/** /**
* *
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override
public void setTitle(String name) {
super.setTitle(name);
}
ConnectFour() { ConnectFour() {
// Constructor // Constructor
@ -50,7 +43,7 @@ public class ConnectFour extends JFrame {
this.getContentPane().setLayout(new GridBagLayout()); this.getContentPane().setLayout(new GridBagLayout());
GameBoard board = new GameBoard(); GameBoard board = new GameBoard();
//board.setPreferredSize(new Dimension(600,600)); board.setPreferredSize(new Dimension(200,200));
this.add(board); this.add(board);
for(int i = 0; i < 7; i++) { 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 static final long serialVersionUID = 1L;
private boolean isInited = false;
/** /**
* The rows of the board * The rows of the board
*/ */
@ -49,7 +47,6 @@ public class GameBoard extends JPanel {
* Constructor * Constructor
*/ */
GameBoard() { GameBoard() {
this.setPreferredSize(new Dimension(this.BoardColumns * 50, this.BoardRows * 50));
this.initBoard(); this.initBoard();
} }
@ -108,35 +105,44 @@ public class GameBoard extends JPanel {
this.add(this.BoardContainers[j][i]); 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 @Override
public void paint(Graphics g) { public void paint(Graphics g) {
System.out.println("updating sizes"); 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[] plateContainers : BoardContainers) {
for (PlateContainer plateContainer : plateContainers) { 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); 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 <= * 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 * 6; j++) { if (GameBoard[i][j] == p && GameBoard[i + 1][j] == p && GameBoard[i

View file

@ -17,18 +17,21 @@
*/ */
package de.itsblue.ConnectFour; package de.itsblue.ConnectFour;
import javax.swing.*;
import java.awt.*; import java.awt.*;
/**
* 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
*/
public class Plate { public class Plate {
/**
*
*/
private static final long serialVersionUID = 1L;
/** /**
* Enum containing all plate types * Enum containing all plate types
*/ */
@ -43,27 +46,52 @@ public class Plate {
/** /**
* Constructor * Constructor
*
* @param type type of the plate * @param type type of the plate
*/ */
Plate(PlateType type) { Plate(PlateType type) {
this.setType(type); this.type = type;
} }
/** /**
* Function to get the type of the plate * Function to get the type of the plate
*
* @return PlateType * @return PlateType
*/ */
public PlateType getType() { public PlateType getType() {
return this.type; return this.type;
} }
/** /**
* 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) { public int getValue() {
this.type = type; 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; package de.itsblue.ConnectFour;
import javax.swing.JPanel; import javax.swing.JPanel;
import de.itsblue.ConnectFour.Plate.PlateType;
import javax.swing.*;
import java.awt.*; import java.awt.*;
public class PlateContainer extends JPanel { public class PlateContainer extends JPanel {
@ -72,7 +68,8 @@ public class PlateContainer extends JPanel {
/** /**
* Function to check if the container is occupied * 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() { public boolean containsPlate() {
if (this.containedPlate != null) if (this.containedPlate != null)
@ -84,7 +81,7 @@ public class PlateContainer extends JPanel {
/** /**
* Function to clear the container * 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() { public Plate removePlate() {
if (!this.containsPlate()) if (!this.containsPlate())
@ -97,16 +94,22 @@ public class PlateContainer extends JPanel {
return ret; return ret;
} }
/**
* Override the paint function to draw the shape of the plate
*/
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
// draw background
g.setColor(Color.lightGray); g.setColor(Color.lightGray);
g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.fillRect(0, 0, this.getWidth(), this.getHeight());
if (this.containsPlate()) { // set color according to contained plate
g.setColor(this.containedPlate.getType() == PlateType.X ? Color.RED : Color.BLACK); if (this.containsPlate())
} else g.setColor(this.containedPlate.getColor());
else
g.setColor(Color.white); g.setColor(Color.white);
// draw plate
g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8), g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
(int) (this.getHeight() * 0.8)); (int) (this.getHeight() * 0.8));
} }