-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoardGUI.java
More file actions
53 lines (41 loc) · 1.4 KB
/
Copy pathChessBoardGUI.java
File metadata and controls
53 lines (41 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
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class ChessBoardGUI{
private JFrame outerFrame;
private JPanel outerPanel;
private GridLayout panelLayout;
private JPanel[][] squares;
public ChessBoardGUI(){
outerFrame = new JFrame("Java Chess");
outerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outerFrame.setVisible(true);
outerFrame.setSize(800, 800);
outerPanel = new JPanel();
outerFrame.getContentPane().add(outerPanel);
panelLayout = new GridLayout(8, 8, 0, 0);
outerPanel.setLayout(panelLayout);
squares = new JPanel[8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
squares[i][j] = new JPanel();
if((i % 2 == 0 && j % 2 == 0) || (i % 2 == 1 && j % 2 == 1)){
squares[i][j].setBackground(Color.white);
}
else{
squares[i][j].setBackground(Color.black);
}
outerPanel.add(squares[i][j]);
}
}
BufferedImage img = null;
try {
img = ImageIO.read(new File("BlackKing.png"));
} catch (IOException e) {
}
JLabel imgLabel = new JLabel(new ImageIcon(img));
squares[2][2].add(imgLabel);
}
}