-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
61 lines (56 loc) · 1.4 KB
/
Board.java
File metadata and controls
61 lines (56 loc) · 1.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel{
public static final int NUM_POSITIONS = 8;
private static JButton [][] board;
private Game game;
public Board(){
Game game = new Game();
Player player = new Player(game);
board = new JButton[NUM_POSITIONS][NUM_POSITIONS];
setLayout(new GridLayout(NUM_POSITIONS,NUM_POSITIONS));
for (int i = 0; i < NUM_POSITIONS; i++){
for (int j = 0; j < NUM_POSITIONS; j++) {
board[i][j] = new JButton();
board[i][j].addActionListener(player);
if((j+i)%2 !=0){
board[i][j].setBackground(Color.RED);
} else {
board[i][j].setBackground(Color.ORANGE);
}
add(board[i][j]);
}
}
game.startGame();
}
/**
* Draws a piece on the board
* @parameter1 the row on the board
* @parameter2 the column on the board
* @parameter3 the color of the player
*/
static void drawPiece(int r, int c,int color){
Piece piece = new Piece();
piece.setColor(color);
board[r][c].setIcon((Icon)piece);
}
/**
* Removes a piece from the board
*/
static void removePiece(int r,int c){
board[r][c].setIcon(null);
}
static int[] getPosition(JButton jb){
for (int i = 0; i < NUM_POSITIONS; i++){
for (int j = 0; j < NUM_POSITIONS; j++){
if(board[i][j].equals(jb)){
int [] position = {i,j};
return position;
}
}
}
return null;
}
}