I have read through and tried several approaches to changing the background color of a JButton with a buffered image, but I am consistently getting the same results. I am using a 2D array of ImageIcons, some with file names such as:
ImageIcon blackRook1 = new ImageIcon("ChessPiece/Chess_rdt60.png");
the png's I am using do not fill the entire square, just the actual chess piece
and others with BufferedImages such as:
ImageIcon space = new ImageIcon(new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
I am then looping through chessBoardSquares, which is an empty 8x8 2D array of JButtons, with:
Insets buttonMargin = new Insets(0, 0, 0, 0);
for(int i = 0; i < chessBoardSquares.length; i++) {
for(int j = 0; j < chessBoardSquares[i].length; j++) {
JButton b = new JButton();
b.setMargin(buttonMargin); // Insets object
ImageIcon icon = this.chessPieces[i][j]; // 2D array of ImageIcons
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);[![enter image description here][1]][1]
Graphics2D g = bi.createGraphics();
//determine color of graphic and jbutton background
int colorDeterminator = j + i;
if(colorDeterminator % 2 == 0) {
// System.out.println("Getting here and coloring white");
b.setBackground(Color.WHITE);
g.setColor(Color.WHITE); // I have also tried with this line commented out
} else {
// System.out.println("Getting here and coloring black");
b.setBackground(Color.BLACK);
g.setColor(Color.BLACK);
}
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
icon.paintIcon(null, g, 0, 0);
g.dispose();
b.setOpaque(true);
b.setIcon(icon);
the if statement using color determinator has definitely run as the print statements have executed
I cannot post images without enough reputation points, so my thought was to post the question in Game Development. I feel that it is more relevant to this channel however, so I will link to that post with the image here.
THE ISSUE There is a square filling most of the JButton, which I believe is the BufferedImage, that is white regardless of what color I set the Graphics2D or the JButton itself. Because I want the whole square to be black in some cases (except for the piece on top of it), I need a way to color the rest of the square.