so basically I am trying to make a a chess/checkers program with GUI, the board is made out of a matrix of 8x8 buttons and i need that when i press i button the button listener will tell me which button was pressed. I didnt add the button listner yet becuase i was hoping you would maybe sujjest a better way to do it
PS: the checkers board is called "chessboard" CODE:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
public class BetterGui {
private final JPanel gui = new JPanel(new BorderLayout(3, 3));
public JButton[][] chessBoardSquares = new JButton[8][8];
private JPanel chessBoard;
private final JLabel message = new JLabel(
"Amos is the King");
private static final String COLS = "ABCDEFGH";
BetterGui(Board board) {
updateGui(board);
}
public final void updateGui(Board board) {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0, 0, 0, 0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
BufferedImage img = null;
try {
if (ii % 2 == jj % 2) {
if (board.GameBoard[ii][jj].ContainingChecker.CheckerType == "red") {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\large_blue_circle.png")); // eventually C:\\ImageTest\\pic2.jpg
} else if (board.GameBoard[ii][jj].ContainingChecker.CheckerType == "black") {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\chcekrplayer.png")); // eventually C:\\ImageTest\\pic2.jpg
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1), SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 0; ii < 8; ii++) {
for (int jj = 0; jj < 8; jj++) {
switch (jj) {
case 0:
chessBoard.add(new JLabel("" + (ii + 1), SwingConstants.CENTER));
default:
chessBoard.add(chessBoardSquares[jj][ii]);
}
}
}
}
public final void initializeGui2() {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0, 0, 0, 0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
BufferedImage img = null;
try {
if (ii % 2 == jj % 2) {
if (ii < 2) {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\large_blue_circle.png")); // eventually C:\\ImageTest\\pic2.jpg
} else if (ii > 4) {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\chcekrplayer.png")); // eventually C:\\ImageTest\\pic2.jpg
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1), SwingConstants.CENTER));
}
}
public final JComponent getChessBoard() {
return chessBoard;
}
public final JComponent getGui() {
return gui;
}
}