From a101632c7b3d06e39886f21373abbb496e633566 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 27 Jul 2019 09:34:40 +0200 Subject: [PATCH] Added board coordinates --- src/dev/kske/chess/game/NaturalPlayer.java | 5 +-- src/dev/kske/chess/ui/BoardComponent.java | 1 + src/dev/kske/chess/ui/BoardPane.java | 4 +- src/dev/kske/chess/ui/MainWindow.java | 49 +++++++++++---------- src/dev/kske/chess/ui/OverlayComponent.java | 1 + 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/dev/kske/chess/game/NaturalPlayer.java b/src/dev/kske/chess/game/NaturalPlayer.java index 4233720..9d12e61 100644 --- a/src/dev/kske/chess/game/NaturalPlayer.java +++ b/src/dev/kske/chess/game/NaturalPlayer.java @@ -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()); diff --git a/src/dev/kske/chess/ui/BoardComponent.java b/src/dev/kske/chess/ui/BoardComponent.java index 07e30b9..09b97d7 100644 --- a/src/dev/kske/chess/ui/BoardComponent.java +++ b/src/dev/kske/chess/ui/BoardComponent.java @@ -27,6 +27,7 @@ public class BoardComponent extends JComponent { public BoardComponent(BoardPane boardPane) { this.boardPane = boardPane; + setSize(boardPane.getPreferredSize()); } @Override diff --git a/src/dev/kske/chess/ui/BoardPane.java b/src/dev/kske/chess/ui/BoardPane.java index 9cd62fe..a36ab97 100644 --- a/src/dev/kske/chess/ui/BoardPane.java +++ b/src/dev/kske/chess/ui/BoardPane.java @@ -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; } diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index 9ec7ef8..45d7872 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -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); diff --git a/src/dev/kske/chess/ui/OverlayComponent.java b/src/dev/kske/chess/ui/OverlayComponent.java index 79526fd..a0be894 100644 --- a/src/dev/kske/chess/ui/OverlayComponent.java +++ b/src/dev/kske/chess/ui/OverlayComponent.java @@ -34,6 +34,7 @@ public class OverlayComponent extends JComponent { public OverlayComponent(BoardPane boardPane) { this.boardPane = boardPane; dots = new ArrayList<>(); + setSize(boardPane.getPreferredSize()); } @Override