lucasjoshua #14
|
@ -7,14 +7,25 @@ import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Klassenbeschreibung
|
* TODO Klassenbeschreibung
|
||||||
|
* reines im frontend zeichnen für preview
|
||||||
*/
|
*/
|
||||||
public class BoardDisplay extends JPanel {
|
public class BoardDisplay extends JPanel {
|
||||||
private JButton[][] fields;
|
private JButton[][] fields;
|
||||||
private int gridSize;
|
private int gridSize;
|
||||||
private List <Ship> ships;
|
//private List <Ship> ships;//brauchen wir nicht mehr
|
||||||
private Ship currentShip;
|
private Ship currentShip;
|
||||||
private Player player;
|
private Player player;
|
||||||
private boolean vertical = false;
|
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.
|
* Konstruktor der startLocalGame.
|
||||||
|
@ -25,7 +36,8 @@ public class BoardDisplay extends JPanel {
|
||||||
public BoardDisplay(int gridSize, Player player) {
|
public BoardDisplay(int gridSize, Player player) {
|
||||||
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte
|
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte
|
||||||
this.fields = new JButton[gridSize][gridSize];
|
this.fields = new JButton[gridSize][gridSize];
|
||||||
this.ships = new ArrayList<>();
|
//this.ships = new ArrayList<>();
|
||||||
|
this.shipButtonList = new ArrayList<>();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.gridSize = gridSize;
|
this.gridSize = gridSize;
|
||||||
System.out.println("Name in Boarddisplay: " + player.getName());//Testausgabe
|
System.out.println("Name in Boarddisplay: " + player.getName());//Testausgabe
|
||||||
|
@ -84,22 +96,21 @@ public class BoardDisplay extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// this.ships = new ArrayList<Ship>();
|
// this.ships = new ArrayList<Ship>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void selectCurrentShip(Ship ship) {
|
public void selectCurrentShip(Ship ship) {
|
||||||
this.currentShip = ship;
|
this.currentShip = ship;
|
||||||
|
paintFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetAllShips() {
|
public void resetAllShips() {
|
||||||
ships.clear();
|
//ships.clear();
|
||||||
this.currentShip = null;
|
this.currentShip = null;
|
||||||
for(int i = 0; i < gridSize; i++) {
|
for (Ship ship : player.getBoard().getShips()) {
|
||||||
for(int j = 0; j < gridSize; j++) {
|
ship.resetPosition();
|
||||||
fields[i][j].setBackground(Color.BLUE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
paintFields();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* TODO Funktion beschreiben etc.
|
* TODO Funktion beschreiben etc.
|
||||||
|
@ -109,31 +120,6 @@ public class BoardDisplay extends JPanel {
|
||||||
* @param player
|
* @param player
|
||||||
* @return
|
* @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.
|
* TODO Funktion beschreiben etc.
|
||||||
|
@ -142,27 +128,6 @@ public class BoardDisplay extends JPanel {
|
||||||
* @param horizontal
|
* @param horizontal
|
||||||
* @return
|
* @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.
|
* Wechselt die Platzierungsrichtung zwischen horizontal und vertikal.
|
||||||
|
@ -190,7 +155,6 @@ public class BoardDisplay extends JPanel {
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void paintFields() {
|
public void paintFields() {
|
||||||
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++) {
|
||||||
|
@ -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.awt.event.MouseAdapter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
// TODO Readybutton farbe Readybutton kontexttext und Spielernamen anzeigen
|
|
||||||
/**
|
/**
|
||||||
* Das GameBoard dient als Panel, in dem das tatsächliche Spiel stattfindet.
|
* Das GameBoard dient als Panel, in dem das tatsächliche Spiel stattfindet.
|
||||||
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
* 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);
|
ShipButton shipButton= new ShipButton(ship,ownBoardPanel);
|
||||||
leftButtonsPanel.add(shipButton);
|
leftButtonsPanel.add(shipButton);
|
||||||
leftButtonGroup.add(shipButton);
|
leftButtonGroup.add(shipButton);
|
||||||
|
ownBoardPanel.addShipButton(shipButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Ship ship : p2.getBoard().getShips()) {
|
for(Ship ship : p2.getBoard().getShips()) {
|
||||||
ShipButton shipButton= new ShipButton(ship,opponentBoardPanel);
|
ShipButton shipButton= new ShipButton(ship,opponentBoardPanel);
|
||||||
rightButtonsPanel.add(shipButton);
|
rightButtonsPanel.add(shipButton);
|
||||||
rightButtonGroup.add(shipButton);
|
rightButtonGroup.add(shipButton);
|
||||||
|
opponentBoardPanel.addShipButton(shipButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
JToggleButton rightPlayerModul7 = new JToggleButton("Bereit");
|
JToggleButton readyButton = new JToggleButton("Bereit");
|
||||||
rightPlayerModul7.setBackground(Color.GREEN);
|
readyButton.setBackground(Color.GREEN);
|
||||||
rightButtonsPanel.add(rightPlayerModul7);
|
rightButtonsPanel.add(readyButton);
|
||||||
|
|
||||||
JToggleButton leftPlayerModul7 = new JToggleButton("Reset");
|
JToggleButton resetButton = new JToggleButton("Reset");
|
||||||
leftPlayerModul7.setBackground(Color.RED);
|
resetButton.setBackground(Color.RED);
|
||||||
leftButtonsPanel.add(leftPlayerModul7);
|
leftButtonsPanel.add(resetButton);
|
||||||
|
|
||||||
leftPlayerModul7.addActionListener(new ActionListener() {
|
resetButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
ownBoardPanel.resetAllShips();
|
ownBoardPanel.resetAllShips();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
rightPlayerModul7.addActionListener(new ActionListener() {
|
readyButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
//TODO richtige funktion einfügen
|
|
||||||
kontextText.setText(kT2);
|
kontextText.setText(kT2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -196,8 +196,8 @@ public class GameBoard extends JPanel {
|
||||||
add(leftButtonsPanel, BorderLayout.WEST);
|
add(leftButtonsPanel, BorderLayout.WEST);
|
||||||
add(rightButtonsPanel, BorderLayout.EAST);
|
add(rightButtonsPanel, BorderLayout.EAST);
|
||||||
add(headerPanel, BorderLayout.NORTH);
|
add(headerPanel, BorderLayout.NORTH);
|
||||||
add(centerPanel, BorderLayout.CENTER);
|
//add(centerPanel, BorderLayout.CENTER);
|
||||||
|
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
||||||
timer.start();
|
timer.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
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.
|
* Der MainFrame dient als Hub und Übergreifendes Fenster auf dem alle weiteren Panel angezeigt werden.
|
||||||
|
|
|
@ -1,18 +1,26 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
public class ShipButton extends JButton {
|
public class ShipButton extends JButton {
|
||||||
Ship ship;
|
private Ship ship;
|
||||||
|
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;
|
||||||
|
this.boardDisplay = boardDisplay;
|
||||||
this.addActionListener((e)->{
|
this.addActionListener((e)->{
|
||||||
boardDisplay.selectCurrentShip(this.ship);
|
boardDisplay.selectCurrentShip(this.ship);
|
||||||
this.setEnabled(false);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buttonPressed() {
|
public void refreshButtonState() {
|
||||||
this.setEnabled(!ship.isPlaced());
|
if(ship.isPlaced()) {
|
||||||
|
setBackground(Color.LIGHT_GRAY);
|
||||||
|
} else {
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
}
|
||||||
|
if(boardDisplay.getCurrentShip() == ship) {
|
||||||
|
setBackground(Color.CYAN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue