0

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.

Same post with image

enter image description here

  • Please clarify -- what is your current code not doing that you *want* it to do? – Hovercraft Full Of Eels Jul 24 '20 at 17:37
  • @HovercraftFullOfEels, sorry, I just added the issue. I had put it in the image description before but then forgot to re add it when I had to get rid of the image. – Diego Esparza Jul 24 '20 at 17:55
  • 1
    Your problem may be with the image itself. Is it truly transparent? If not then what you're trying to do won't work. – Hovercraft Full Of Eels Jul 24 '20 at 18:03
  • @HovercraftFullOfEels Before putting the image file path into the ImageIcon constructor, the image was transparent. I downloaded the images from this wikipedia link https://commons.wikimedia.org/wiki/Category:PNG_chess_pieces/Standard_transparent and in Atom (text editor) when I open the image it is transparent (around the piece). And thank you for commenting – Diego Esparza Jul 24 '20 at 18:57
  • Seemed to work OK for me using `ImageIO.read(...)`: [PasteBin.com](https://pastebin.com/Jh48A8cr) – Hovercraft Full Of Eels Jul 24 '20 at 19:44
  • @HovercraftFullOfEels thank you very much for such detailed help! I have tried around a couple of different ways of using a JButton instead of a JPanel, such as adding the JLabel to a JButton or setting the ImageIcon to the JButton with .setIcon(), but I keep getting the same results as before. Do you know of an easy way to apply this to a button instead? Sorry to keep following up, and I'm grateful for the help you've already offered. – Diego Esparza Jul 24 '20 at 21:31
  • See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Jul 25 '20 at 08:57
  • I don't know why you would want to make the background of a square black? When you add a black chess piece to a black square you will not see the chess piece. Normally you would use a neutral color for the dark color so that both the black and white chess pieces will show properly. Then you would typically use a JLabel which is transparent by default and add your icon to the label and the label to the cell. Check out: https://stackoverflow.com/a/6811800/131872 for a simple example using this approach – camickr Jul 25 '20 at 17:55

0 Answers0