lucasjoshua #16
|
@ -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);
|
||||||
|
@ -80,13 +81,25 @@ public class BoardDisplay extends JPanel {
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
field.addMouseListener(new MouseAdapter() {
|
field.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
mousePosition = new Point(x, y);
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
paintFields();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
if (SwingUtilities.isRightMouseButton(e)) {
|
if (SwingUtilities.isRightMouseButton(e)) {
|
||||||
togglePlacementDirection(); // Ausrichtung ändern
|
togglePlacementDirection();
|
||||||
} else if (SwingUtilities.isLeftMouseButton(e)) {
|
} else if (SwingUtilities.isLeftMouseButton(e)) {
|
||||||
Point o = new Point(x, y);
|
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) {
|
public void selectCurrentShip(Ship ship) {
|
||||||
this.currentShip = ship;
|
this.currentShip = ship;
|
||||||
paintFields();
|
paintFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zuruecksetzen von aktuellem Schiff und allen Schiffen des Spielers
|
||||||
|
* Danach blau faerben des Spielfeldes
|
||||||
|
*/
|
||||||
public void resetAllShips() {
|
public void resetAllShips() {
|
||||||
//ships.clear();
|
//ships.clear();
|
||||||
this.currentShip = null;
|
this.currentShip = null;
|
||||||
|
@ -110,22 +131,6 @@ public class BoardDisplay extends JPanel {
|
||||||
}
|
}
|
||||||
paintFields();
|
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.
|
* Wechselt die Platzierungsrichtung zwischen horizontal und vertikal.
|
||||||
|
@ -134,6 +139,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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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() {
|
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() && 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()) {
|
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);
|
||||||
|
@ -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() {
|
public void refresh() {
|
||||||
paintFields();
|
paintFields();
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
// import javafx.scene.control.ToggleGroup;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
@ -20,10 +18,6 @@ public class GameBoard extends JPanel {
|
||||||
private Player p1;
|
private Player p1;
|
||||||
private Player p2;
|
private Player p2;
|
||||||
|
|
||||||
|
|
||||||
// Funktionshilfen
|
|
||||||
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
|
|
||||||
|
|
||||||
// Grafiken
|
// Grafiken
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.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 kT6 = "Dein Gegner ist am Zug";
|
||||||
String kT7 = "Du hast das Spiel gewonnen";
|
String kT7 = "Du hast das Spiel gewonnen";
|
||||||
String kT8 = "Du hast das Spiel verloren";
|
String kT8 = "Du hast das Spiel verloren";
|
||||||
|
String kT9 = "Bitte erst alle Schiffe setzten";
|
||||||
|
|
||||||
// Labels
|
// Labels
|
||||||
JLabel frameTitle = new JLabel("GameBoard");
|
JLabel frameTitle = new JLabel("GameBoard");
|
||||||
|
@ -61,18 +56,15 @@ public class GameBoard extends JPanel {
|
||||||
List<Ship> shipsP2 =p2.getBoard().getShips();
|
List<Ship> shipsP2 =p2.getBoard().getShips();
|
||||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
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) {
|
private void updateButtonLabels(List<Ship> ships,JToggleButton[] buttons) {
|
||||||
for(int i=0;i<buttons.length &&i<ships.size();i++ ) {
|
for(int i=0;i<buttons.length &&i<ships.size();i++ ) {
|
||||||
Ship ship = ships.get(i);
|
Ship ship = ships.get(i);
|
||||||
buttons[i].setText(ship.getName());
|
buttons[i].setText(ship.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Timer für pulsierenden Effekt
|
// Timer für pulsierenden Effekt
|
||||||
Timer timer = new Timer(10, new ActionListener() {
|
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.
|
* TODO Funktion beschreiben etc.
|
||||||
* @param frame
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
* @param semesterCounter
|
* @param semesterCounter Ausgewähltes Semester
|
||||||
*/
|
*/
|
||||||
public void buildPanel(MainFrame frame, int semesterCounter) {
|
public void buildPanel(MainFrame frame, int semesterCounter) {
|
||||||
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
// 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
|
//Buttons in eine Gruppe packen damit diese beim drücken eines anderen Buttons wieder entwählt werden
|
||||||
ButtonGroup leftButtonGroup= new ButtonGroup();
|
ButtonGroup leftButtonGroup= new ButtonGroup();
|
||||||
|
|
||||||
ButtonGroup rightButtonGroup= new ButtonGroup();
|
ButtonGroup rightButtonGroup= new ButtonGroup();
|
||||||
|
|
||||||
// Panel für die Buttons des linken Spielers (ganz links)
|
// Panel für die Buttons des linken Spielers (ganz links)
|
||||||
|
@ -170,10 +180,6 @@ public class GameBoard extends JPanel {
|
||||||
p1.ready();
|
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)
|
// Panel für beide Spielfelder (nebeneinander in der Mitte)
|
||||||
JPanel centerPanel = new JPanel();
|
JPanel centerPanel = new JPanel();
|
||||||
|
@ -210,6 +216,9 @@ public class GameBoard extends JPanel {
|
||||||
timer.start();
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
if (this.p1.myTurn) {
|
if (this.p1.myTurn) {
|
||||||
this.kontextText.setText(kT5);
|
this.kontextText.setText(kT5);
|
||||||
|
|
|
@ -97,12 +97,7 @@ public class Ship {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
// Liste an Punkten die das Schiff einnehmen wuerde
|
||||||
List<Point> shipPoints = new ArrayList<>();
|
List<Point> shipPoints = this.getVirtualOccupiedPoints(pos, horizontal);
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ueberlappen mit anderen Schiffen pruefen
|
// ueberlappen mit anderen Schiffen pruefen
|
||||||
for (Ship otherShip : shipsList) {
|
for (Ship otherShip : shipsList) {
|
||||||
|
@ -126,6 +121,22 @@ public class Ship {
|
||||||
return true;
|
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() {
|
public List<Point> getOccupiedPoints() {
|
||||||
List<Point> points = new ArrayList<>();
|
List<Point> points = new ArrayList<>();
|
||||||
if (this.position == null) {
|
if (this.position == null) {
|
||||||
|
|
|
@ -4,6 +4,7 @@ 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;
|
||||||
|
|
Loading…
Reference in New Issue