From a13cf8d05092eb47ade29298fb72ef44a9931387 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 27 Jul 2019 08:06:43 +0200 Subject: [PATCH] Added border around board, changed display order in OverlayComponent --- src/dev/kske/chess/game/NaturalPlayer.java | 5 ++- src/dev/kske/chess/ui/BoardComponent.java | 1 - src/dev/kske/chess/ui/BoardPane.java | 26 ++++-------- src/dev/kske/chess/ui/OverlayComponent.java | 47 ++++++++++----------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/dev/kske/chess/game/NaturalPlayer.java b/src/dev/kske/chess/game/NaturalPlayer.java index 9d12e61..4233720 100644 --- a/src/dev/kske/chess/game/NaturalPlayer.java +++ b/src/dev/kske/chess/game/NaturalPlayer.java @@ -1,5 +1,6 @@ package dev.kske.chess.game; +import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.List; @@ -51,7 +52,9 @@ public class NaturalPlayer extends Player implements MouseListener { @Override public void mousePressed(MouseEvent evt) { - if (!moveRequested) return; + Rectangle boardRect = new Rectangle(overlayComponent.getWidth() - overlayComponent.getX(), + overlayComponent.getHeight() - overlayComponent.getY()); + if (!moveRequested || !boardRect.contains(evt.getPoint())) 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 09b97d7..07e30b9 100644 --- a/src/dev/kske/chess/ui/BoardComponent.java +++ b/src/dev/kske/chess/ui/BoardComponent.java @@ -27,7 +27,6 @@ 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 a2349db..9cd62fe 100644 --- a/src/dev/kske/chess/ui/BoardPane.java +++ b/src/dev/kske/chess/ui/BoardPane.java @@ -1,8 +1,6 @@ package dev.kske.chess.ui; import java.awt.Dimension; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; import javax.swing.JLayeredPane; @@ -23,29 +21,21 @@ 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, Integer.valueOf(1)); - add(overlayComponent, Integer.valueOf(2)); + add(boardComponent); + add(overlayComponent); - /* - * Add a component listener for adjusting the tile size on resizing. - * The size of the board is assumed to be 8x8, as well as the both the board and - * the tiles being square. - */ - addComponentListener(new ComponentAdapter() { - - @Override - public void componentResized(ComponentEvent e) { - tileSize = getWidth() / 8; - TextureUtil.scalePieceTextures(tileSize); - } - }); + tileSize = 60; setSize(getPreferredSize()); } @Override - public Dimension getPreferredSize() { return new Dimension(480, 480); } + public Dimension getPreferredSize() { return new Dimension(560, 560); } public BoardComponent getBoardComponent() { return boardComponent; } diff --git a/src/dev/kske/chess/ui/OverlayComponent.java b/src/dev/kske/chess/ui/OverlayComponent.java index 723f1af..79526fd 100644 --- a/src/dev/kske/chess/ui/OverlayComponent.java +++ b/src/dev/kske/chess/ui/OverlayComponent.java @@ -28,12 +28,11 @@ public class OverlayComponent extends JComponent { private final BoardPane boardPane; - private List dots; - private Move arrow; + private List dots; + private Move arrow; public OverlayComponent(BoardPane boardPane) { this.boardPane = boardPane; - setSize(boardPane.getPreferredSize()); dots = new ArrayList<>(); } @@ -43,20 +42,12 @@ public class OverlayComponent extends JComponent { final int tileSize = getTileSize(); - // Draw possible moves if a piece was selected - if (!dots.isEmpty()) { - g.setColor(Color.green); - int radius = tileSize / 4; - for (Position dot : dots) - g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2, dot.y * tileSize + tileSize / 2 - radius / 2, - radius, radius); - } - - // Draw an arrow representing the last move and mark its position and destination + // Draw an arrow representing the last move and mark its position and + // destination if (arrow != null) { - Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.y * tileSize + tileSize / 2); - Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2); - + Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.y * tileSize + tileSize / 2); + Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2); + Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(3)); @@ -70,7 +61,17 @@ public class OverlayComponent extends JComponent { g2d.setColor(Color.black); g2d.draw(arrowShape); } - + + // Draw possible moves if a piece was selected + if (!dots.isEmpty()) { + g.setColor(Color.green); + int radius = tileSize / 4; + for (Position dot : dots) + g.fillOval(dot.x * tileSize + tileSize / 2 - radius / 2, + dot.y * tileSize + tileSize / 2 - radius / 2, + radius, + radius); + } } private Shape createArrowShape(Point pos, Point dest) { @@ -85,10 +86,10 @@ public class OverlayComponent extends JComponent { Point midPoint = midpoint(pos, dest); - double rotate = Math.atan2(dest.y - pos.y, dest.x - pos.x); - double ptDistance = pos.distance(dest); - double scale = ptDistance / 12.0; // 12 because it's the length of the arrow - // polygon. + double rotate = Math.atan2(dest.y - pos.y, dest.x - pos.x); + double ptDistance = pos.distance(dest); + double scale = ptDistance / 12.0; // 12 because it's the length of the arrow + // polygon. AffineTransform transform = new AffineTransform(); @@ -124,7 +125,5 @@ public class OverlayComponent extends JComponent { repaint(); } - public int getTileSize() { - return boardPane.getTileSize(); - } + public int getTileSize() { return boardPane.getTileSize(); } }