Added border around board, changed display order in OverlayComponent
This commit is contained in:
parent
6d85a01fc2
commit
a13cf8d050
@ -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());
|
||||||
|
@ -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
|
||||||
|
@ -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; }
|
||||||
|
|
||||||
|
@ -28,12 +28,11 @@ public class OverlayComponent extends JComponent {
|
|||||||
|
|
||||||
private final BoardPane boardPane;
|
private final BoardPane boardPane;
|
||||||
|
|
||||||
private List<Position> dots;
|
private List<Position> dots;
|
||||||
private Move arrow;
|
private Move arrow;
|
||||||
|
|
||||||
public OverlayComponent(BoardPane boardPane) {
|
public OverlayComponent(BoardPane boardPane) {
|
||||||
this.boardPane = boardPane;
|
this.boardPane = boardPane;
|
||||||
setSize(boardPane.getPreferredSize());
|
|
||||||
dots = new ArrayList<>();
|
dots = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,20 +42,12 @@ 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);
|
||||||
|
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
g2d.setStroke(new BasicStroke(3));
|
g2d.setStroke(new BasicStroke(3));
|
||||||
|
|
||||||
@ -70,7 +61,17 @@ public class OverlayComponent extends JComponent {
|
|||||||
g2d.setColor(Color.black);
|
g2d.setColor(Color.black);
|
||||||
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) {
|
||||||
@ -85,10 +86,10 @@ public class OverlayComponent extends JComponent {
|
|||||||
|
|
||||||
Point midPoint = midpoint(pos, dest);
|
Point midPoint = midpoint(pos, dest);
|
||||||
|
|
||||||
double rotate = Math.atan2(dest.y - pos.y, dest.x - pos.x);
|
double rotate = Math.atan2(dest.y - pos.y, dest.x - pos.x);
|
||||||
double ptDistance = pos.distance(dest);
|
double ptDistance = pos.distance(dest);
|
||||||
double scale = ptDistance / 12.0; // 12 because it's the length of the arrow
|
double scale = ptDistance / 12.0; // 12 because it's the length of the arrow
|
||||||
// polygon.
|
// polygon.
|
||||||
|
|
||||||
AffineTransform transform = new AffineTransform();
|
AffineTransform transform = new AffineTransform();
|
||||||
|
|
||||||
@ -124,7 +125,5 @@ public class OverlayComponent extends JComponent {
|
|||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTileSize() {
|
public int getTileSize() { return boardPane.getTileSize(); }
|
||||||
return boardPane.getTileSize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user