JButton 背景颜色和图片

huangapple 未分类评论44阅读模式
英文:

JButton background color AND image

问题

我已阅读并尝试了几种方法来改变带有缓冲图像的JButton的背景颜色,但结果始终如一。我正在使用一个二维的ImageIcons数组,其中一些文件名如下:

ImageIcon blackRook1 = new ImageIcon("ChessPiece/Chess_rdt60.png");

我使用的png图片不填充整个方块,只有实际的棋子部分。

而另一些带有BufferedImages的如下:

ImageIcon space = new ImageIcon(new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));

然后,我通过循环遍历chessBoardSquares,这是一个空的8x8的JButtons二维数组,代码如下:

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对象
        ImageIcon icon = this.chessPieces[i][j]; // 二维ImageIcons数组
        BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = bi.createGraphics();

        // 确定图形和JButton背景的颜色
        int colorDeterminator = j + i;
        if(colorDeterminator % 2 == 0) {
            b.setBackground(Color.WHITE);
            g.setColor(Color.WHITE); // 我也尝试过将这一行注释掉
        } else {
            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);

使用颜色判定器的if语句确实已运行,因为打印语句已执行。

我无法在没有足够声望积分的情况下发布图片,因此我的想法是将问题发布到游戏开发中。然而,我觉得这个问题更适合此频道,所以我会在此处附上带有图片的链接。

问题所在
在JButton的大部分区域都有一个方块,我认为那是BufferedImage,而且无论我如何设置Graphics2D或JButton本身的颜色,它始终是白色的。因为我希望在某些情况下整个方块都是黑色的(除了上面的棋子),我需要找到一种方法来对方块的其余部分进行着色。

英文:

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(&quot;ChessPiece/Chess_rdt60.png&quot;);

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 &lt; chessBoardSquares.length; i++) {
      for(int j = 0; j &lt; 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(&quot;Getting here and coloring white&quot;);
          b.setBackground(Color.WHITE);
          g.setColor(Color.WHITE); // I have also tried with this line commented out
        } else {
          // System.out.println(&quot;Getting here and coloring black&quot;);
          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

JButton 背景颜色和图片

huangapple
  • 本文由 发表于 2020年7月25日 01:34:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/63078701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定