Browse Source

Changed whole GUI layout to two matchfields

Lorenz Hohmann 3 years ago
parent
commit
96c14b288e
  1. 305
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java
  2. 2
      src/test/java/de/tims/fleetstorm/gui/LogicTest.java

305
src/main/java/de/tims/fleetstorm/gui/GameLogic.java

@ -1,8 +1,6 @@
package de.tims.fleetstorm.gui; package de.tims.fleetstorm.gui;
import java.awt.Color; import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,234 +29,86 @@ public class GameLogic extends JPanel {
public static final int OVER = 3; public static final int OVER = 3;
// GUI stuff // GUI stuff
private ArrayList<JPanel> fields;
private JLabel matchfieldValue;
private JLabel stateValue;
private JLabel own2Ship;
private JLabel own3Ship;
private JLabel own4Ship;
private JLabel own5Ship;
private JLabel enemy2Ship;
private JLabel enemy3Ship;
private JLabel enemy4Ship;
private JLabel enemy5Ship;
public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) {
this.fields = new ArrayList<>();
private ArrayList<JPanel> playerFields;
private ArrayList<JPanel> enemyFields;
public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) {
this.playerFields = new ArrayList<>();
this.enemyFields = new ArrayList<>();
setSize(640, 480); setSize(640, 480);
JPanel fieldWrapper = new JPanel(); JPanel fieldWrapper = new JPanel();
fieldWrapper.setBounds(10, 11, 362, 458);
fieldWrapper.setBounds(10, 11, 305, 458);
fieldWrapper.setLayout(null); fieldWrapper.setLayout(null);
JPanel infoWrapper = new JPanel();
infoWrapper.setBounds(382, 11, 248, 458);
infoWrapper.setLayout(null);
setLayout(null); setLayout(null);
add(fieldWrapper); add(fieldWrapper);
add(infoWrapper);
JPanel yourFleet = new JPanel();
yourFleet.setBounds(10, 110, 228, 93);
infoWrapper.add(yourFleet);
yourFleet.setLayout(null);
JPanel ownFleetHeadingPanel = new JPanel();
ownFleetHeadingPanel.setBounds(0, 0, 228, 27);
yourFleet.add(ownFleetHeadingPanel);
ownFleetHeadingPanel.setLayout(null);
JLabel ownFleetHeading = new JLabel("Deine Flotte:");
ownFleetHeading.setBounds(0, 0, 228, 17);
ownFleetHeadingPanel.add(ownFleetHeading);
ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14));
JPanel ownFleetContentPanel = new JPanel();
ownFleetContentPanel.setBounds(0, 26, 228, 67);
yourFleet.add(ownFleetContentPanel);
ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3));
JLabel own2ShipLabel = new JLabel("1x2 Schiff");
ownFleetContentPanel.add(own2ShipLabel);
own2Ship = new JLabel("OK");
ownFleetContentPanel.add(own2Ship);
JLabel own3ShipLabel = new JLabel("1x3 Schiff");
ownFleetContentPanel.add(own3ShipLabel);
own3Ship = new JLabel("OK");
ownFleetContentPanel.add(own3Ship);
JLabel own4ShipLabel = new JLabel("1x4 Schiff");
ownFleetContentPanel.add(own4ShipLabel);
own4Ship = new JLabel("OK");
ownFleetContentPanel.add(own4Ship);
JLabel own5ShipLabel = new JLabel("1x5 Schiff");
ownFleetContentPanel.add(own5ShipLabel);
own5Ship = new JLabel("OK");
ownFleetContentPanel.add(own5Ship);
JPanel enemyFleet = new JPanel();
enemyFleet.setLayout(null);
enemyFleet.setBounds(10, 237, 228, 93);
infoWrapper.add(enemyFleet);
JPanel enemyFleetHeadingPanel = new JPanel();
enemyFleetHeadingPanel.setLayout(null);
enemyFleetHeadingPanel.setBounds(0, 0, 228, 27);
enemyFleet.add(enemyFleetHeadingPanel);
JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:");
enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14));
enemyFleetHeading.setBounds(0, 0, 228, 17);
enemyFleetHeadingPanel.add(enemyFleetHeading);
JPanel enemyFleetContentPanel = new JPanel();
enemyFleetContentPanel.setBounds(0, 26, 228, 67);
enemyFleet.add(enemyFleetContentPanel);
enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3));
JPanel enemyFieldWrapper = new JPanel();
enemyFieldWrapper.setLayout(null);
enemyFieldWrapper.setBounds(326, 11, 305, 458);
add(enemyFieldWrapper);
JLabel enemy2ShipLabel = new JLabel("1x2 Schiff");
enemyFleetContentPanel.add(enemy2ShipLabel);
this.generateFieldsForGUI(gapToFrameBorderX, gapToFrameBorderY, fieldWidth, spaceBetween, fieldWrapper,
this.playerFields, false);
enemy2Ship = new JLabel("OK");
enemyFleetContentPanel.add(enemy2Ship);
JLabel ownFieldLabel = new JLabel("Eigenes Spielfeld");
ownFieldLabel.setBounds(10, 11, 114, 14);
fieldWrapper.add(ownFieldLabel);
this.generateFieldsForGUI(gapToFrameBorderX, gapToFrameBorderY, fieldWidth, spaceBetween, enemyFieldWrapper,
this.enemyFields, true);
JLabel enemy3ShipLabel = new JLabel("1x3 Schiff");
enemyFleetContentPanel.add(enemy3ShipLabel);
enemy3Ship = new JLabel("OK");
enemyFleetContentPanel.add(enemy3Ship);
JLabel enemy4ShipLabel = new JLabel("1x4 Schiff");
enemyFleetContentPanel.add(enemy4ShipLabel);
enemy4Ship = new JLabel("OK");
enemyFleetContentPanel.add(enemy4Ship);
JLabel enemy5ShipLabel = new JLabel("1x5 Schiff");
enemyFleetContentPanel.add(enemy5ShipLabel);
enemy5Ship = new JLabel("OK");
enemyFleetContentPanel.add(enemy5Ship);
JPanel infos = new JPanel();
infos.setLayout(null);
infos.setBounds(10, 11, 228, 68);
infoWrapper.add(infos);
JPanel gameInfoHeadingPanel = new JPanel();
gameInfoHeadingPanel.setLayout(null);
gameInfoHeadingPanel.setBounds(0, 0, 228, 27);
infos.add(gameInfoHeadingPanel);
JLabel gameInfoHeading = new JLabel("Spielinfos");
gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14));
gameInfoHeading.setBounds(0, 0, 228, 17);
gameInfoHeadingPanel.add(gameInfoHeading);
JPanel gameInfoContentPanel = new JPanel();
gameInfoContentPanel.setBounds(0, 26, 228, 31);
infos.add(gameInfoContentPanel);
gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3));
JLabel matchfieldLabel = new JLabel("Spielfeld");
gameInfoContentPanel.add(matchfieldLabel);
matchfieldValue = new JLabel("-");
gameInfoContentPanel.add(matchfieldValue);
JLabel stateLabel = new JLabel("Status");
gameInfoContentPanel.add(stateLabel);
stateValue = new JLabel("-");
gameInfoContentPanel.add(stateValue);
JLabel enemyFieldLabel = new JLabel("Gegnerisches Spielfeld");
enemyFieldLabel.setBounds(10, 11, 168, 14);
enemyFieldWrapper.add(enemyFieldLabel);
}
private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween,
JPanel panel, ArrayList<JPanel> fields, boolean registerMouseListener) {
for (int x = 0; x < 10; x++) { for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) { for (int y = 0; y < 10; y++) {
JPanel field = new JPanel(); JPanel field = new JPanel();
int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween);
int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween);
int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween);
int yPos = gapToFrameBorderY + y * fieldWidth + (y * spaceBetween);
field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBounds(xPos, yPos, fieldWidth, fieldWidth);
field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
field.setName(x + ":" + y); field.setName(x + ":" + y);
fields.add(field); fields.add(field);
fieldWrapper.add(field);
panel.add(field);
field.addMouseListener(new MouseAdapter() {
if (registerMouseListener) {
field.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (!playerMove)
return;
public void mouseClicked(MouseEvent e) {
System.out.println("mouse clicked");
if (!playerMove)
return;
int[] coords = getCoordinateFromLabel(field);
int coordX = coords[0];
int coordY = coords[1];
int[] coords = getCoordinateFromLabel(field);
int coordX = coords[0];
int coordY = coords[1];
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY);
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY);
boolean shotSuccess = chosenField.shoot();
gui.updateField(enemyMatchfield);
boolean shotSuccess = chosenField.shoot();
gui.updateFields();
if (shotSuccess && chosenField.getState() != Coordinate.HIT) {
nextMove(false);
}
if (shotSuccess && chosenField.getState() != Coordinate.HIT) {
nextMove(false);
} else {
nextMove(true);
}
}
});
}
});
}
} }
} }
} }
public ArrayList<JPanel> getFields() { public ArrayList<JPanel> getFields() {
return fields;
}
public JLabel getMatchfieldValue() {
return matchfieldValue;
}
public JLabel getStateValue() {
return stateValue;
}
public JLabel getOwn2Ship() {
return own2Ship;
}
public JLabel getOwn3Ship() {
return own3Ship;
}
public JLabel getOwn4Ship() {
return own4Ship;
}
public JLabel getOwn5Ship() {
return own5Ship;
}
public JLabel getEnemy2Ship() {
return enemy2Ship;
}
public JLabel getEnemy3Ship() {
return enemy3Ship;
}
public JLabel getEnemy4Ship() {
return enemy4Ship;
}
public JLabel getEnemy5Ship() {
return enemy5Ship;
return playerFields;
} }
private JLabel getXLabel() { private JLabel getXLabel() {
@ -276,9 +126,15 @@ public class GameLogic extends JPanel {
return new int[] { coordX, coordY }; return new int[] { coordX, coordY };
} }
private void updateField(Matchfield matchfield) {
for (JPanel coordinatePanel : this.fields) {
int[] coords = this.getCoordinateFromLabel(coordinatePanel);
private void updateFields() {
this.updateField(enemyMatchfield, this.enemyFields);
this.updateField(matchfield, this.playerFields);
}
private void updateField(Matchfield matchfield, ArrayList<JPanel> fields) {
for (JPanel coordinatePanel : fields) {
int[] coords = getCoordinateFromLabel(coordinatePanel);
int coordX = coords[0]; int coordX = coords[0];
int coordY = coords[1]; int coordY = coords[1];
@ -298,7 +154,7 @@ public class GameLogic extends JPanel {
break; break;
case Coordinate.HIT: case Coordinate.HIT:
coordinatePanel.setBackground(new Color(91, 58, 41)); coordinatePanel.setBackground(new Color(91, 58, 41));
coordinatePanel.add(this.getXLabel());
coordinatePanel.add(getXLabel());
break; break;
} }
@ -306,21 +162,6 @@ public class GameLogic extends JPanel {
coordinatePanel.repaint(); coordinatePanel.repaint();
} }
// set game infos
if (this.playerMove) {
System.out.println("own update");
if (this.gameState == GameLogic.PREPARATION) {
this.getMatchfieldValue().setText("Eigenes");
this.getStateValue().setText("Vorbereiten...");
} else {
this.getMatchfieldValue().setText("Gegner");
this.getStateValue().setText("Du bist am Zug");
}
} else {
System.out.println("enemy update");
this.getMatchfieldValue().setText("Eigenes");
this.getStateValue().setText("Gegner am Zug");
}
} }
/** /**
@ -330,9 +171,10 @@ public class GameLogic extends JPanel {
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame("Test GUI"); JFrame frame = new JFrame("Test GUI");
gui = new GameLogic(15, 28, 1);
gui = new GameLogic(12, 35, 25, 1);
frame.setContentPane(gui); frame.setContentPane(gui);
frame.setSize(640, 480); frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true); frame.setResizable(true);
frame.setVisible(true); frame.setVisible(true);
@ -361,15 +203,6 @@ public class GameLogic extends JPanel {
this.aiLogic = new Logic(); this.aiLogic = new Logic();
this.aiLogic.setMatchfield(this.matchfield); this.aiLogic.setMatchfield(this.matchfield);
// show player own matchfield
gui.updateField(this.matchfield);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// enter game loop // enter game loop
this.gameState = GameLogic.RUNNING; this.gameState = GameLogic.RUNNING;
this.nextMove(true); this.nextMove(true);
@ -379,29 +212,17 @@ public class GameLogic extends JPanel {
this.playerMove = playerMove; this.playerMove = playerMove;
if (this.playerMove) { if (this.playerMove) {
gui.updateField(this.enemyMatchfield);
gui.updateFields();
// waiting for mouse click event.... // waiting for mouse click event....
} else { } else {
gui.updateField(this.matchfield);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Coordinate aiChosenField = this.aiLogic.chooseField(); Coordinate aiChosenField = this.aiLogic.chooseField();
aiChosenField.print();
aiChosenField.shoot(); aiChosenField.shoot();
gui.updateField(this.matchfield);
gui.updateFields();
boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT; boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.nextMove(aiHasNextMove); this.nextMove(aiHasNextMove);
} }

2
src/test/java/de/tims/fleetstorm/gui/LogicTest.java

@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
class LogicTest { class LogicTest {
GameLogic guiLogic = new GameLogic(0, 0, 0);
GameLogic guiLogic = new GameLogic(0, 0, 0, 0);
@BeforeEach @BeforeEach
void setup() { void setup() {

Loading…
Cancel
Save