-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceClicking.java
More file actions
47 lines (43 loc) · 1.44 KB
/
PieceClicking.java
File metadata and controls
47 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* PieceClicking.java
* Justin Granofsky & Jerry Yu
* 2018-05-24
* Version 1.0
* Handles displaying possible moves
*/
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PieceClicking implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Make sure that the component being clicked is a JButton
if (e.getSource() instanceof JButton) {
// Cast the source to a JButton
JButton button = (JButton) e.getSource();
// Get the x coordinate from the button's text.
int x = Integer.parseInt(button.getText().substring(0,
button.getText().indexOf(":")));
// Get the y coordinate from the button's text.
int y = Integer.parseInt(button.getText().substring(
button.getText().indexOf(":") + 1));
// Get the piece that was clicked.
CheckerPiece piece = Checkers.getPiece(x, y);
// Make sure the piece exists.
if (piece == null) {
return;
}
// If the piece is already selected, don't repaint possible moves.
if (piece.equals(Board.selected)) {
return;
}
// Set the selected piece to be the clicked piece.
Board.selected = piece;
// If the possible moves aren't empty...
if (!piece.getPossibleMoves().isEmpty())
// Draw all possible moves for the given checker piece.
Checkers.board.drawPossibleMoves(Checkers.board.getGraphics(),
piece);
}
}
}