Added board coordinates

This commit is contained in:
Kai S. K. Engelbart 2019-07-27 09:34:40 +02:00
parent 51558797cc
commit 1f5242935f
5 changed files with 29 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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

View File

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