pewpew #17
|
@ -18,6 +18,7 @@ public class BoardDisplay extends JPanel {
|
|||
private boolean horizontal = false;
|
||||
private List<ShipButton> shipButtonList;
|
||||
private boolean enemyBoard;
|
||||
private Point mousePosition;
|
||||
|
||||
public void addShipButton(ShipButton button) {
|
||||
shipButtonList.add(button);
|
||||
|
@ -82,13 +83,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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -99,11 +112,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;
|
||||
|
@ -112,22 +133,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.
|
||||
|
@ -136,6 +141,7 @@ public class BoardDisplay extends JPanel {
|
|||
horizontal = !horizontal;
|
||||
String direction = horizontal ? "horizontal" : "vertikal";
|
||||
System.out.println("Platzierungsrichtung geändert zu: " + direction);
|
||||
paintFields();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,7 +166,17 @@ 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<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) {
|
||||
|
@ -181,6 +197,13 @@ public class BoardDisplay extends JPanel {
|
|||
}
|
||||
}
|
||||
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);
|
||||
|
@ -207,13 +230,7 @@ public class BoardDisplay extends JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
private void previewShipPlacement(int startX, int startY, boolean vertical) {
|
||||
//TODO schreiben
|
||||
}
|
||||
|
||||
private void clearPreview() {
|
||||
|
||||
}
|
||||
|
||||
public void refresh() {
|
||||
paintFields();
|
||||
|
|
|
@ -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<Ship> 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<Ship> ships,JToggleButton[] buttons) {
|
||||
for(int i=0;i<buttons.length &&i<ships.size();i++ ) {
|
||||
Ship ship = ships.get(i);
|
||||
buttons[i].setText(ship.getName());
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Timer für pulsierenden Effekt
|
||||
Timer timer = new Timer(10, new ActionListener() {
|
||||
|
@ -99,10 +91,29 @@ public class GameBoard extends JPanel {
|
|||
}
|
||||
});
|
||||
|
||||
/*
|
||||
// Timer für pulsierenden Disco-Effekt
|
||||
Timer timer = new Timer(50, new ActionListener() {
|
||||
private float hue = 0; // Farbton-Wert für HSB-Farbmodell
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// Farbe basierend auf dem Farbton-Wert berechnen
|
||||
Color pulsierendeFarbe = Color.getHSBColor(hue, 0.8f, 0.8f); // Sättigung und Helligkeit fix
|
||||
kontextText.setForeground(pulsierendeFarbe);
|
||||
|
||||
// Farbton leicht verändern (Zyklus zwischen 0 und 1)
|
||||
hue += 0.01f;
|
||||
if (hue > 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);
|
||||
|
|
|
@ -97,12 +97,7 @@ public class Ship {
|
|||
}
|
||||
|
||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
||||
List<Point> 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<Point> 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<Point> getVirtualOccupiedPoints(Point pos, boolean horizontal) {
|
||||
List<Point> 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<Point> getOccupiedPoints() {
|
||||
List<Point> points = new ArrayList<>();
|
||||
if (this.position == null) {
|
||||
|
|
|
@ -4,22 +4,23 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue