From b4f1de82bd6583f1e19716f060440b55cdbf058c Mon Sep 17 00:00:00 2001 From: Kaver Date: Sun, 15 Dec 2024 15:25:53 +0100 Subject: [PATCH] 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