From 7038a4c607685de72392d63b326897e5885761aa Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:08:23 +0100 Subject: [PATCH] Added default ship positioning to GUI --- .../de/tims/fleetstorm/gui/GameLogic.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index d00b7a9..0d6d14f 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -13,6 +13,7 @@ import javax.swing.JPanel; import javax.swing.border.MatteBorder; import de.tims.fleetstorm.ai.Logic; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; public class GameLogic extends JPanel { @@ -186,6 +187,7 @@ public class GameLogic extends JPanel { field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + field.setName(x + ":" + y); fields.add(field); fieldWrapper.add(field); @@ -246,16 +248,38 @@ public class GameLogic extends JPanel { return enemy5Ship; } + private void updateField(boolean player, Matchfield matchfield) { + for (JPanel coordinatePanel : this.fields) { + String panelName = coordinatePanel.getName(); + String[] panelNameSplit = panelName.split(":"); + int coordX = Integer.parseInt(panelNameSplit[0]); + int coordY = Integer.parseInt(panelNameSplit[1]); + + matchfield.getField(coordX, coordY).print(); + int state = matchfield.getField(coordX, coordY).getState(); + + if (state == Coordinate.SHIP) + coordinatePanel.setBackground(Color.GREEN); + + coordinatePanel.revalidate(); + coordinatePanel.repaint(); + } + } + /** * This function is only for testing */ + private static GameLogic gui; + public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GameLogic gui = new GameLogic(15, 28, 1); + gui = new GameLogic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); frame.setVisible(true); + + gui.start(); } public void start() { @@ -263,11 +287,24 @@ public class GameLogic extends JPanel { // create player matchfield this.matchfield = new Matchfield(matchfieldSize); + this.matchfield.createMatchfield(); + this.matchfield.setShip(new Coordinate(4, 6), 2, 0); + this.matchfield.setShip(new Coordinate(1, 3), 3, 0); + this.matchfield.setShip(new Coordinate(9, 1), 4, 1); + this.matchfield.setShip(new Coordinate(0, 0), 5, 0); // create enemy matchfield and initialize AI (with player's matchfield) this.enemyMatchfield = new Matchfield(matchfieldSize); + this.enemyMatchfield.createMatchfield(); + this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); + this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); + this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); + this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); this.aiLogic = new Logic(); this.aiLogic.setMatchfield(this.matchfield); + + // show player own matchfield + gui.updateField(true, this.matchfield); } public void nextMove() {