Added board coordinates

This commit is contained in:
Kai S. K. Engelbart 2019-07-27 09:34:40 +02:00
parent 596a4afcc7
commit 884fcd2722
5 changed files with 29 additions and 31 deletions

View File

@ -1,6 +1,5 @@
package dev.kske.chess.game;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
@ -52,9 +51,7 @@ public class NaturalPlayer extends Player implements MouseListener {
@Override
public void mousePressed(MouseEvent evt) {
Rectangle boardRect = new Rectangle(overlayComponent.getWidth() - overlayComponent.getX(),
overlayComponent.getHeight() - overlayComponent.getY());
if (!moveRequested || !boardRect.contains(evt.getPoint())) return;
if (!moveRequested) return;
if (pos == null) {
pos = new Position(evt.getPoint().x / overlayComponent.getTileSize(),
evt.getPoint().y / overlayComponent.getTileSize());

View File

@ -27,6 +27,7 @@ public class BoardComponent extends JComponent {
public BoardComponent(BoardPane boardPane) {
this.boardPane = boardPane;
setSize(boardPane.getPreferredSize());
}
@Override

View File

@ -21,10 +21,8 @@ public class BoardPane extends JLayeredPane {
public BoardPane() {
boardComponent = new BoardComponent(this);
boardComponent.setBounds(40, 40, 520, 520);
overlayComponent = new OverlayComponent(this);
setLayer(overlayComponent, 1);
overlayComponent.setBounds(40, 40, 520, 520);
setLayout(null);
add(boardComponent);
@ -35,7 +33,7 @@ public class BoardPane extends JLayeredPane {
}
@Override
public Dimension getPreferredSize() { return new Dimension(560, 560); }
public Dimension getPreferredSize() { return new Dimension(480, 480); }
public BoardComponent getBoardComponent() { return boardComponent; }

View File

@ -2,10 +2,12 @@ package dev.kske.chess.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import dev.kske.chess.board.Piece.Color;
@ -20,11 +22,11 @@ import dev.kske.chess.game.NaturalPlayer;
*/
public class MainWindow {
private JFrame mframe;
private JButton btnRestart, btnSwapColors;
private BoardPane boardPane;
private Game game;
private Color activeColor;
private JFrame mframe;
private JButton btnRestart, btnSwapColors;
private BoardPane boardPane;
private Game game;
private Color activeColor;
/**
* Launch the application.
@ -58,24 +60,17 @@ public class MainWindow {
mframe.setResizable(false);
mframe.setBounds(100, 100, 494, 565);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mframe.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/pieces/queen_white.png")));
boardPane = new BoardPane();
mframe.getContentPane().add(boardPane, BorderLayout.CENTER);
JPanel toolPanel = new JPanel();
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> {
if (game != null)
game.reset();
game.start();
});
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
activeColor = Color.WHITE;
btnSwapColors = new JButton("Play as black");
activeColor = Color.WHITE;
btnSwapColors = new JButton("Play as black");
btnSwapColors.addActionListener((evt) -> {
game.swapColors();
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
@ -84,24 +79,30 @@ public class MainWindow {
toolPanel.add(btnRestart);
toolPanel.add(btnSwapColors);
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
JPanel letterPanel = new JPanel(new GridLayout(1, 8));
for (int i = 0; i < 8; i++)
letterPanel.add(new JLabel(String.valueOf((char) (65 + i))));
mframe.add(letterPanel, BorderLayout.SOUTH);
JPanel numberPanel = new JPanel(new GridLayout(8, 1));
for (int i = 0; i < 8; i++)
numberPanel.add(new JLabel(String.valueOf(8 - i)));
mframe.add(numberPanel, BorderLayout.EAST);
mframe.setJMenuBar(new MenuBar(this));
mframe.pack();
mframe.setLocationRelativeTo(null);
}
public BoardPane getBoardPane() {
return boardPane;
}
public BoardPane getBoardPane() { return boardPane; }
public Game getGame() {
return game;
}
public Game getGame() { return game; }
public void setGame(Game game) {
if (this.game != null)
this.game.disconnect();
if (this.game != null) this.game.disconnect();
this.game = game;
btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);

View File

@ -34,6 +34,7 @@ public class OverlayComponent extends JComponent {
public OverlayComponent(BoardPane boardPane) {
this.boardPane = boardPane;
dots = new ArrayList<>();
setSize(boardPane.getPreferredSize());
}
@Override