Preview des setzen von Schiffen

This commit is contained in:
Kaver 2024-12-15 15:25:53 +01:00
parent 60e44f2e1a
commit b4f1de82bd
2 changed files with 21 additions and 18 deletions

View File

@ -17,6 +17,7 @@ public class BoardDisplay extends JPanel {
private Player player; private Player player;
private boolean horizontal = false; private boolean horizontal = false;
private List<ShipButton> shipButtonList; private List<ShipButton> shipButtonList;
private Point mousePosition;
public void addShipButton(ShipButton button) { public void addShipButton(ShipButton button) {
shipButtonList.add(button); shipButtonList.add(button);
@ -82,16 +83,14 @@ public class BoardDisplay extends JPanel {
field.addMouseListener(new MouseAdapter() { field.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
if (currentShip != null) { mousePosition = new Point(x, y);
previewShipPlacement(x, y, horizontal); paintFields();
}
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
if (currentShip != null) { paintFields();
clearPreview();
}
} }
@Override @Override
@ -100,7 +99,7 @@ public class BoardDisplay extends JPanel {
togglePlacementDirection(); togglePlacementDirection();
} else if (SwingUtilities.isLeftMouseButton(e)) { } else if (SwingUtilities.isLeftMouseButton(e)) {
Point o = new Point(x, y); Point o = new Point(x, y);
currentShip.setHorizontal(horizontal); handleFieldClick(o);
} }
} }
}); });
@ -148,6 +147,7 @@ public class BoardDisplay extends JPanel {
horizontal = !horizontal; horizontal = !horizontal;
String direction = horizontal ? "horizontal" : "vertikal"; String direction = horizontal ? "horizontal" : "vertikal";
System.out.println("Platzierungsrichtung geändert zu: " + direction); System.out.println("Platzierungsrichtung geändert zu: " + direction);
paintFields();
} }
/** /**
@ -168,12 +168,21 @@ public class BoardDisplay extends JPanel {
} }
public void paintFields() { public void paintFields() {
List<Point> test=new ArrayList<>();
if(currentShip != null) {
test = currentShip.getVirtualOccupiedPoints(mousePosition, horizontal);
}
for(int i = 0; i < gridSize; i++) { for(int i = 0; i < gridSize; i++) {
for(int j = 0; j < gridSize; j++) { for(int j = 0; j < gridSize; j++) {
if(fields[i][j] == null) { if(fields[i][j] == null) {
continue; continue;
} }
fields[i][j].setBackground(Color.BLUE); 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()) { for(Ship ship: player.getBoard().getShips()) {
if(ship.isShipOnPos(new Point(i,j))) { if(ship.isShipOnPos(new Point(i,j))) {
fields[i][j].setBackground(Color.LIGHT_GRAY); 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() { public void refresh() {

View File

@ -4,23 +4,24 @@ import java.awt.*;
public class ShipButton extends JButton { public class ShipButton extends JButton {
private Ship ship; private Ship ship;
private BoardDisplay boardDisplay; private BoardDisplay boardDisplay;
public ShipButton(Ship ship, BoardDisplay boardDisplay) { public ShipButton(Ship ship, BoardDisplay boardDisplay) {
super(ship.getName()); super(ship.getName());
this.ship = ship; this.ship = ship;
this.boardDisplay = boardDisplay; this.boardDisplay = boardDisplay;
this.addActionListener((e)->{ this.addActionListener((e) -> {
boardDisplay.selectCurrentShip(this.ship); boardDisplay.selectCurrentShip(this.ship);
}); });
} }
public void refreshButtonState() { public void refreshButtonState() {
if(ship.isPlaced()) { if (ship.isPlaced()) {
setBackground(Color.LIGHT_GRAY); setBackground(Color.LIGHT_GRAY);
} else { } else {
setBackground(Color.WHITE); setBackground(Color.WHITE);
} }
if(boardDisplay.getCurrentShip() == ship) { if (boardDisplay.getCurrentShip() == ship) {
setBackground(Color.CYAN); setBackground(Color.CYAN);
} }
} }
} }