Preview des setzen von Schiffen
This commit is contained in:
parent
60e44f2e1a
commit
b4f1de82bd
|
@ -17,6 +17,7 @@ public class BoardDisplay extends JPanel {
|
|||
private Player player;
|
||||
private boolean horizontal = false;
|
||||
private List<ShipButton> 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<Point> 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() {
|
||||
|
|
|
@ -4,6 +4,7 @@ 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;
|
||||
|
|
Loading…
Reference in New Issue