diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index 7d10ab1..ddad939 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); @@ -80,13 +81,25 @@ public class BoardDisplay extends JPanel { // } // }); field.addMouseListener(new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) { + mousePosition = new Point(x, y); + paintFields(); + } + + @Override + public void mouseExited(MouseEvent e) { + paintFields(); + + } + @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 + handleFieldClick(o); } } }); @@ -97,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; @@ -110,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. @@ -134,6 +139,7 @@ public class BoardDisplay extends JPanel { horizontal = !horizontal; String direction = horizontal ? "horizontal" : "vertikal"; System.out.println("Platzierungsrichtung geändert zu: " + direction); + paintFields(); } /** @@ -153,13 +159,30 @@ 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) { + 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() && 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()) { if(ship.isShipOnPos(new Point(i,j))) { fields[i][j].setBackground(Color.LIGHT_GRAY); @@ -173,13 +196,7 @@ public class BoardDisplay extends JPanel { } } - private void previewShipPlacement(int startX, int startY, boolean vertical) { - //TODO schreiben - } - private void clearPreview() { - - } public void refresh() { paintFields(); 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); 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) { 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