Fixed showing Player names in GameBoard.
Added functionality to Button coloring (when selected, placed etc.)
This commit is contained in:
parent
d1dbcfe603
commit
970550308b
|
@ -7,14 +7,25 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* TODO Klassenbeschreibung
|
||||
* reines im frontend zeichnen für preview
|
||||
*/
|
||||
public class BoardDisplay extends JPanel {
|
||||
private JButton[][] fields;
|
||||
private int gridSize;
|
||||
private List <Ship> ships;
|
||||
//private List <Ship> ships;//brauchen wir nicht mehr
|
||||
private Ship currentShip;
|
||||
private Player player;
|
||||
private boolean vertical = false;
|
||||
private List<ShipButton> shipButtonList;
|
||||
|
||||
public void addShipButton(ShipButton button) {
|
||||
shipButtonList.add(button);
|
||||
paintFields();
|
||||
}
|
||||
|
||||
public Ship getCurrentShip() {
|
||||
return currentShip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Konstruktor der startLocalGame.
|
||||
|
@ -25,7 +36,8 @@ public class BoardDisplay extends JPanel {
|
|||
public BoardDisplay(int gridSize, Player player) {
|
||||
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte
|
||||
this.fields = new JButton[gridSize][gridSize];
|
||||
this.ships = new ArrayList<>();
|
||||
//this.ships = new ArrayList<>();
|
||||
this.shipButtonList = new ArrayList<>();
|
||||
this.player = player;
|
||||
this.gridSize = gridSize;
|
||||
System.out.println("Name in Boarddisplay: " + player.getName());//Testausgabe
|
||||
|
@ -84,22 +96,21 @@ public class BoardDisplay extends JPanel {
|
|||
}
|
||||
}
|
||||
// this.ships = new ArrayList<Ship>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void selectCurrentShip(Ship ship) {
|
||||
this.currentShip = ship;
|
||||
paintFields();
|
||||
}
|
||||
|
||||
public void resetAllShips() {
|
||||
ships.clear();
|
||||
//ships.clear();
|
||||
this.currentShip = null;
|
||||
for(int i = 0; i < gridSize; i++) {
|
||||
for(int j = 0; j < gridSize; j++) {
|
||||
fields[i][j].setBackground(Color.BLUE);
|
||||
}
|
||||
for (Ship ship : player.getBoard().getShips()) {
|
||||
ship.resetPosition();
|
||||
}
|
||||
paintFields();
|
||||
}
|
||||
/**
|
||||
* TODO Funktion beschreiben etc.
|
||||
|
@ -109,31 +120,6 @@ public class BoardDisplay extends JPanel {
|
|||
* @param player
|
||||
* @return
|
||||
*/
|
||||
private boolean setShip(Ship ship, Point o, boolean horizontal,Player player) {
|
||||
//boolean a = true;
|
||||
if (placeable(ship, ship.getPosition(), horizontal)) {
|
||||
ship.setPosition(new Point(o.getX(),o.getY()),player.getBoard().getShips(),gridSize);
|
||||
ship.setHorizontal(horizontal);
|
||||
ships.add(ship);
|
||||
List<Point> occupied = ship.getOccupiedPoints();
|
||||
for(Point p: occupied) {
|
||||
fields[p.getX()][p.getY()].setBackground(Color.LIGHT_GRAY);
|
||||
}
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*private boolean placeable(Ship ship,Point o, boolean horizontal) {
|
||||
if (horizontal && (o.getX() + ship.getSize() > gridSize)) { // Fehler
|
||||
return false;
|
||||
}
|
||||
if (!horizontal && (o.getY() + ship.getSize() > gridSize)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* TODO Funktion beschreiben etc.
|
||||
|
@ -142,27 +128,6 @@ public class BoardDisplay extends JPanel {
|
|||
* @param horizontal
|
||||
* @return
|
||||
*/
|
||||
private boolean placeable(Ship ship, Point o, boolean horizontal) {
|
||||
if (horizontal && (o.getX() + ship.getSize() > gridSize)) {
|
||||
return false;
|
||||
}
|
||||
if (!horizontal && (o.getY() + ship.getSize() > gridSize)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prüfen auf Kollision mit bestehenden Schiffen
|
||||
for (Ship other : ships) {
|
||||
for (Point p : other.getOccupiedPoints()) {
|
||||
for (Point newP : ship.getOccupiedPoints()) {
|
||||
if (p.equals(newP)) {
|
||||
return false; // Überschneidung gefunden
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wechselt die Platzierungsrichtung zwischen horizontal und vertikal.
|
||||
|
@ -190,7 +155,6 @@ public class BoardDisplay extends JPanel {
|
|||
//}
|
||||
}
|
||||
|
||||
|
||||
public void paintFields() {
|
||||
for(int i = 0; i < gridSize; i++) {
|
||||
for(int j = 0; j < gridSize; j++) {
|
||||
|
@ -206,5 +170,17 @@ public class BoardDisplay extends JPanel {
|
|||
}
|
||||
}
|
||||
}
|
||||
for( ShipButton shipButton: shipButtonList) {
|
||||
shipButton.refreshButtonState();
|
||||
}
|
||||
}
|
||||
|
||||
private void previewShipPlacement(int startX, int startY, boolean vertical) {
|
||||
//TODO schreiben
|
||||
}
|
||||
|
||||
private void clearPreview() {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -8,8 +8,6 @@ import java.awt.event.MouseEvent;
|
|||
import java.awt.event.MouseAdapter;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
// TODO Readybutton farbe Readybutton kontexttext und Spielernamen anzeigen
|
||||
/**
|
||||
* Das GameBoard dient als Panel, in dem das tatsächliche Spiel stattfindet.
|
||||
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
||||
|
@ -131,33 +129,35 @@ public class GameBoard extends JPanel {
|
|||
ShipButton shipButton= new ShipButton(ship,ownBoardPanel);
|
||||
leftButtonsPanel.add(shipButton);
|
||||
leftButtonGroup.add(shipButton);
|
||||
ownBoardPanel.addShipButton(shipButton);
|
||||
}
|
||||
|
||||
for(Ship ship : p2.getBoard().getShips()) {
|
||||
ShipButton shipButton= new ShipButton(ship,opponentBoardPanel);
|
||||
rightButtonsPanel.add(shipButton);
|
||||
rightButtonGroup.add(shipButton);
|
||||
opponentBoardPanel.addShipButton(shipButton);
|
||||
}
|
||||
|
||||
JToggleButton rightPlayerModul7 = new JToggleButton("Bereit");
|
||||
rightPlayerModul7.setBackground(Color.GREEN);
|
||||
rightButtonsPanel.add(rightPlayerModul7);
|
||||
JToggleButton readyButton = new JToggleButton("Bereit");
|
||||
readyButton.setBackground(Color.GREEN);
|
||||
rightButtonsPanel.add(readyButton);
|
||||
|
||||
JToggleButton leftPlayerModul7 = new JToggleButton("Reset");
|
||||
leftPlayerModul7.setBackground(Color.RED);
|
||||
leftButtonsPanel.add(leftPlayerModul7);
|
||||
JToggleButton resetButton = new JToggleButton("Reset");
|
||||
resetButton.setBackground(Color.RED);
|
||||
leftButtonsPanel.add(resetButton);
|
||||
|
||||
leftPlayerModul7.addActionListener(new ActionListener() {
|
||||
resetButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ownBoardPanel.resetAllShips();
|
||||
}
|
||||
});
|
||||
|
||||
rightPlayerModul7.addActionListener(new ActionListener() {
|
||||
readyButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
//TODO richtige funktion einfügen
|
||||
|
||||
kontextText.setText(kT2);
|
||||
}
|
||||
});
|
||||
|
@ -196,8 +196,8 @@ public class GameBoard extends JPanel {
|
|||
add(leftButtonsPanel, BorderLayout.WEST);
|
||||
add(rightButtonsPanel, BorderLayout.EAST);
|
||||
add(headerPanel, BorderLayout.NORTH);
|
||||
add(centerPanel, BorderLayout.CENTER);
|
||||
|
||||
//add(centerPanel, BorderLayout.CENTER);
|
||||
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
||||
timer.start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
//TODO Generell Buttons in GameBoard anpassen
|
||||
//TODO Generell button ausblenden anpassen
|
||||
|
||||
/**
|
||||
* Der MainFrame dient als Hub und Übergreifendes Fenster auf dem alle weiteren Panel angezeigt werden.
|
||||
|
|
|
@ -1,18 +1,26 @@
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class ShipButton extends JButton {
|
||||
Ship ship;
|
||||
|
||||
private Ship ship;
|
||||
private BoardDisplay boardDisplay;
|
||||
public ShipButton(Ship ship, BoardDisplay boardDisplay) {
|
||||
super(ship.getName());
|
||||
this.ship = ship;
|
||||
this.boardDisplay = boardDisplay;
|
||||
this.addActionListener((e)->{
|
||||
boardDisplay.selectCurrentShip(this.ship);
|
||||
this.setEnabled(false);
|
||||
});
|
||||
}
|
||||
|
||||
public void buttonPressed() {
|
||||
this.setEnabled(!ship.isPlaced());
|
||||
public void refreshButtonState() {
|
||||
if(ship.isPlaced()) {
|
||||
setBackground(Color.LIGHT_GRAY);
|
||||
} else {
|
||||
setBackground(Color.WHITE);
|
||||
}
|
||||
if(boardDisplay.getCurrentShip() == ship) {
|
||||
setBackground(Color.CYAN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue