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 6d85a01fc2
commit a13cf8d050
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 35 additions and 44 deletions

View File

@ -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());

View File

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

View File

@ -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; }

View File

@ -28,12 +28,11 @@ public class OverlayComponent extends JComponent {
private final BoardPane boardPane;
private List<Position> dots;
private Move arrow;
private List<Position> dots;
private Move arrow;
public OverlayComponent(BoardPane boardPane) {
this.boardPane = boardPane;
setSize(boardPane.getPreferredSize());
dots = new ArrayList<>();
}
@ -43,19 +42,11 @@ 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));
@ -71,6 +62,16 @@ public class OverlayComponent extends JComponent {
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(); }
}