Added border around board, changed display order in OverlayComponent

This commit is contained in:
Kai S. K. Engelbart 2019-07-27 08:06:43 +02:00
parent ae38e67a90
commit 51558797cc
4 changed files with 35 additions and 44 deletions

View File

@ -1,5 +1,6 @@
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;
@ -51,7 +52,9 @@ public class NaturalPlayer extends Player implements MouseListener {
@Override @Override
public void mousePressed(MouseEvent evt) { 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) { 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,7 +27,6 @@ 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

@ -1,8 +1,6 @@
package dev.kske.chess.ui; package dev.kske.chess.ui;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLayeredPane; import javax.swing.JLayeredPane;
@ -23,29 +21,21 @@ 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);
overlayComponent.setBounds(40, 40, 520, 520);
setLayout(null);
add(boardComponent, Integer.valueOf(1)); add(boardComponent);
add(overlayComponent, Integer.valueOf(2)); add(overlayComponent);
/* tileSize = 60;
* 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);
}
});
setSize(getPreferredSize()); setSize(getPreferredSize());
} }
@Override @Override
public Dimension getPreferredSize() { return new Dimension(480, 480); } public Dimension getPreferredSize() { return new Dimension(560, 560); }
public BoardComponent getBoardComponent() { return boardComponent; } public BoardComponent getBoardComponent() { return boardComponent; }

View File

@ -33,7 +33,6 @@ public class OverlayComponent extends JComponent {
public OverlayComponent(BoardPane boardPane) { public OverlayComponent(BoardPane boardPane) {
this.boardPane = boardPane; this.boardPane = boardPane;
setSize(boardPane.getPreferredSize());
dots = new ArrayList<>(); dots = new ArrayList<>();
} }
@ -43,16 +42,8 @@ public class OverlayComponent extends JComponent {
final int tileSize = getTileSize(); final int tileSize = getTileSize();
// Draw possible moves if a piece was selected // Draw an arrow representing the last move and mark its position and
if (!dots.isEmpty()) { // destination
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
if (arrow != null) { if (arrow != null) {
Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.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); Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2);
@ -71,6 +62,16 @@ public class OverlayComponent extends JComponent {
g2d.draw(arrowShape); 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) { private Shape createArrowShape(Point pos, Point dest) {
@ -124,7 +125,5 @@ public class OverlayComponent extends JComponent {
repaint(); repaint();
} }
public int getTileSize() { public int getTileSize() { return boardPane.getTileSize(); }
return boardPane.getTileSize();
}
} }