From 7605646b80107b5da9272c86cffef0d29f486976 Mon Sep 17 00:00:00 2001 From: Kaver Date: Sun, 15 Dec 2024 14:51:40 +0100 Subject: [PATCH 1/6] Action Listener Update fuer BoardDisplay --- src/BoardDisplay.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index 7d10ab1..d06563c 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -80,13 +80,27 @@ public class BoardDisplay extends JPanel { // } // }); field.addMouseListener(new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) { + if (currentShip != null) { + previewShipPlacement(x, y, horizontal); + } + } + + @Override + public void mouseExited(MouseEvent e) { + if (currentShip != null) { + clearPreview(); + } + } + @Override public void mouseClicked(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { - togglePlacementDirection(); // Ausrichtung ändern + togglePlacementDirection(); } else if (SwingUtilities.isLeftMouseButton(e)) { Point o = new Point(x, y); - handleFieldClick(o); // Linksklick -> Schiff platzieren + currentShip.setHorizontal(horizontal); } } }); @@ -181,6 +195,7 @@ public class BoardDisplay extends JPanel { } + public void refresh() { paintFields(); } From 60e44f2e1aad3f14c460e5244cd06de660e81d81 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Sun, 15 Dec 2024 15:10:20 +0100 Subject: [PATCH 2/6] add getVirtualOccupiedPoints --- src/Ship.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Ship.java b/src/Ship.java index 5dfac8c..7dd7459 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -97,12 +97,7 @@ public class Ship { } // Liste an Punkten die das Schiff einnehmen wuerde - List shipPoints = new ArrayList<>(); - for (int i = 0; i < this.size; i++) { - int x = horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x - int y = horizontal ? pos.getY() : pos.getY() + i; - shipPoints.add(new Point(x, y)); - } + List shipPoints = this.getVirtualOccupiedPoints(pos, horizontal); // ueberlappen mit anderen Schiffen pruefen for (Ship otherShip : shipsList) { @@ -126,6 +121,22 @@ public class Ship { return true; } + /** + * Returns the Points on the ship if it were to be placed at positino `pos` in orientation defined by `horizontal` + */ + public List getVirtualOccupiedPoints(Point pos, boolean horizontal) { + List points = new ArrayList<>(); + if (pos == null) { + return points; + } + for (int i = 0; i < this.size; i++) { + int x = horizontal ? pos.getX() + i : pos.getX(); + int y = horizontal ? pos.getY() : pos.getY() + i; + points.add(new Point(x, y)); + } + return points; + } + public List getOccupiedPoints() { List points = new ArrayList<>(); if (this.position == null) { From b4f1de82bd6583f1e19716f060440b55cdbf058c Mon Sep 17 00:00:00 2001 From: Kaver Date: Sun, 15 Dec 2024 15:25:53 +0100 Subject: [PATCH 3/6] Preview des setzen von Schiffen --- src/BoardDisplay.java | 30 ++++++++++++++++-------------- src/ShipButton.java | 9 +++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index d06563c..ca2e327 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -17,6 +17,7 @@ public class BoardDisplay extends JPanel { private Player player; private boolean horizontal = false; private List shipButtonList; + private Point mousePosition; public void addShipButton(ShipButton button) { shipButtonList.add(button); @@ -82,16 +83,14 @@ public class BoardDisplay extends JPanel { field.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { - if (currentShip != null) { - previewShipPlacement(x, y, horizontal); - } + mousePosition = new Point(x, y); + paintFields(); } @Override public void mouseExited(MouseEvent e) { - if (currentShip != null) { - clearPreview(); - } + paintFields(); + } @Override @@ -100,7 +99,7 @@ public class BoardDisplay extends JPanel { togglePlacementDirection(); } else if (SwingUtilities.isLeftMouseButton(e)) { Point o = new Point(x, y); - currentShip.setHorizontal(horizontal); + handleFieldClick(o); } } }); @@ -148,6 +147,7 @@ public class BoardDisplay extends JPanel { horizontal = !horizontal; String direction = horizontal ? "horizontal" : "vertikal"; System.out.println("Platzierungsrichtung geändert zu: " + direction); + paintFields(); } /** @@ -168,12 +168,21 @@ public class BoardDisplay extends JPanel { } public void paintFields() { + List test=new ArrayList<>(); + if(currentShip != null) { + test = currentShip.getVirtualOccupiedPoints(mousePosition, horizontal); + } for(int i = 0; i < gridSize; i++) { for(int j = 0; j < gridSize; j++) { if(fields[i][j] == null) { continue; } fields[i][j].setBackground(Color.BLUE); + for(Point p : test) { + if(i==p.getX() && j==p.getY()) { + fields[i][j].setBackground(Color.YELLOW); + } + } for(Ship ship: player.getBoard().getShips()) { if(ship.isShipOnPos(new Point(i,j))) { fields[i][j].setBackground(Color.LIGHT_GRAY); @@ -187,13 +196,6 @@ public class BoardDisplay extends JPanel { } } - private void previewShipPlacement(int startX, int startY, boolean vertical) { - //TODO schreiben - } - - private void clearPreview() { - - } public void refresh() { diff --git a/src/ShipButton.java b/src/ShipButton.java index e282676..bbc79fa 100644 --- a/src/ShipButton.java +++ b/src/ShipButton.java @@ -4,23 +4,24 @@ import java.awt.*; public class ShipButton extends JButton { private Ship ship; private BoardDisplay boardDisplay; + public ShipButton(Ship ship, BoardDisplay boardDisplay) { super(ship.getName()); this.ship = ship; this.boardDisplay = boardDisplay; - this.addActionListener((e)->{ + this.addActionListener((e) -> { boardDisplay.selectCurrentShip(this.ship); }); } public void refreshButtonState() { - if(ship.isPlaced()) { + if (ship.isPlaced()) { setBackground(Color.LIGHT_GRAY); } else { setBackground(Color.WHITE); } - if(boardDisplay.getCurrentShip() == ship) { + if (boardDisplay.getCurrentShip() == ship) { setBackground(Color.CYAN); } } -} +} \ No newline at end of file From ad61fcecb7b9ccbc38bdb9f788d7809bcbc04344 Mon Sep 17 00:00:00 2001 From: Kaver Date: Sun, 15 Dec 2024 15:51:32 +0100 Subject: [PATCH 4/6] Anpasung an paintFields, sodass preview unterscheidet, ob setzen von Schiff valide ist --- src/BoardDisplay.java | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index ca2e327..1bb501e 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -110,11 +110,19 @@ public class BoardDisplay extends JPanel { } + /** + * Aktuelles Schiff auswaehlen + * @param ship + */ public void selectCurrentShip(Ship ship) { this.currentShip = ship; paintFields(); } + /** + * Zuruecksetzen von aktuellem Schiff und allen Schiffen des Spielers + * Danach blau faerben des Spielfeldes + */ public void resetAllShips() { //ships.clear(); this.currentShip = null; @@ -123,22 +131,6 @@ public class BoardDisplay extends JPanel { } paintFields(); } - /** - * TODO Funktion beschreiben etc. - * @param ship - * @param o - * @param horizontal - * @param player - * @return - */ - - /** - * TODO Funktion beschreiben etc. - * @param ship - * @param o - * @param horizontal - * @return - */ /** * Wechselt die Platzierungsrichtung zwischen horizontal und vertikal. @@ -179,8 +171,10 @@ public class BoardDisplay extends JPanel { } fields[i][j].setBackground(Color.BLUE); for(Point p : test) { - if(i==p.getX() && j==p.getY()) { - fields[i][j].setBackground(Color.YELLOW); + if(i==p.getX() && j==p.getY() && currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) { + fields[i][j].setBackground(Color.GREEN); + }else if(i==p.getX() && j==p.getY() && !currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) { + fields[i][j].setBackground(Color.RED); } } for(Ship ship: player.getBoard().getShips()) { From c0f22cec6e1e3fc20de62ba138a2b7d1e66c80c5 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 15 Dec 2024 16:01:11 +0100 Subject: [PATCH 5/6] Cleaned up code --- src/GameBoard.java | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/GameBoard.java b/src/GameBoard.java index 0f22fa7..725c1d0 100644 --- a/src/GameBoard.java +++ b/src/GameBoard.java @@ -1,5 +1,3 @@ -// import javafx.scene.control.ToggleGroup; - import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -20,10 +18,6 @@ public class GameBoard extends JPanel { private Player p1; private Player p2; - - // Funktionshilfen - //int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel - // Grafiken ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png"); ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png"); @@ -38,6 +32,7 @@ public class GameBoard extends JPanel { String kT6 = "Dein Gegner ist am Zug"; String kT7 = "Du hast das Spiel gewonnen"; String kT8 = "Du hast das Spiel verloren"; + String kT9 = "Bitte erst alle Schiffe setzten"; // Labels JLabel frameTitle = new JLabel("GameBoard"); @@ -61,18 +56,15 @@ public class GameBoard extends JPanel { List shipsP2 =p2.getBoard().getShips(); backButton.addActionListener(e -> frame.showPanel("MainMenu")); } +/* TODO löschen falls nicht gebraucht - /** - * TODO Funktion beschreiben etc. - * @param ships - * @param buttons - */ private void updateButtonLabels(List ships,JToggleButton[] buttons) { for(int i=0;i 1) { + hue = 0; + } + } + });*/ + /** * TODO Funktion beschreiben etc. - * @param frame - * @param semesterCounter + * @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden. + * @param semesterCounter Ausgewähltes Semester */ public void buildPanel(MainFrame frame, int semesterCounter) { // Hauptlayout - BorderLayout für die Anordnung der Komponenten @@ -129,7 +140,6 @@ public class GameBoard extends JPanel { //Buttons in eine Gruppe packen damit diese beim drücken eines anderen Buttons wieder entwählt werden ButtonGroup leftButtonGroup= new ButtonGroup(); - ButtonGroup rightButtonGroup= new ButtonGroup(); // Panel für die Buttons des linken Spielers (ganz links) @@ -170,10 +180,6 @@ public class GameBoard extends JPanel { p1.ready(); } }); - // Panel für die Buttons des rechten Spielers (ganz rechts) - - //JPanel ownBoardPanel = new JPanel(new GridLayout(gridSize, gridSize)); - //JPanel opponentBoardPanel = new JPanel(new GridLayout(gridSize, gridSize)); // Panel für beide Spielfelder (nebeneinander in der Mitte) JPanel centerPanel = new JPanel(); @@ -210,6 +216,9 @@ public class GameBoard extends JPanel { timer.start(); } + /** + * + */ public void refresh() { if (this.p1.myTurn) { this.kontextText.setText(kT5); From 37a984772cb150938f8506a5e73ff875561b539c Mon Sep 17 00:00:00 2001 From: Kaver Date: Sun, 15 Dec 2024 16:01:52 +0100 Subject: [PATCH 6/6] Javadoc Kommentare erweitert in BoardDisplay --- src/BoardDisplay.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index 1bb501e..ddad939 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -159,6 +159,12 @@ public class BoardDisplay extends JPanel { //} } + /** + * Faerbt das Spielfeld blau, ueberprueft ob das aktuelle Schiff null ist + * Faerbt eine Preview in das Spielfeld ein ob Schiff setzen moeglich ist + * Schiffe die gesetzt wurden werden grau gefaerbt + * Aufrufen von refreshButtonState um zu zeigen ob Button drueckbar ist + */ public void paintFields() { List test=new ArrayList<>(); if(currentShip != null) {