0

I am trying to add an image on top of a button using swing. I have looked at other similar questions on stackoverflow, and have tried to do this is a few different ways. Unfortunately none have been successful. To be specific, I have looked at this tutorial, this question and this question.

The relevant code is this:

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Icon;



public class GameBoard extends JPanel implements ActionListener {

private JButton B1, B2, B3, B4, B5, B6, B7, B8, B9; 
private GameArray GArray; 
private boolean Player = false;
private int PlayerMark = 1;

GameBoard() {

    setLayout(new GridLayout(3, 3));

    B1 = new JButton();
    ...
    B9 = new JButton();

    SetGame();

    add(B1);
    ...
    add(B9);

    B1.addActionListener(this);
    ...
    B9.addActionListener(this);

}

public void SetGame() {

    GArray = new GameArray(this);

    DefaultText();
    DisableAll(true);

    Player = false; 
    PlayerMark = 1;
}

public void Reset() {

    SetGame(); 

}

public void actionPerformed(ActionEvent E) {

    ...

}

//Other methods

public void SetText(JButton Btn, boolean Play) {

    if (Play == true) {
        Btn.setText("O");

    } else if (Play == false) {

        Btn.setIcon(new ImageIcon(GameBoard.class.getResource("red.png")));

    }
  }
}

What I was hoping to happen is that after I press one of the nine buttons on the first go, the button will have the image "red.png" on it and will no longer be enabled. However, what actually happens is that is just becomes disabled and turns a dark grey.

What I'm guessing is happening is that the image is underneath the button - is this correct? If so, how would I change it so that the image can be seen on the button?

Community
  • 1
  • 1
Elise
  • 131
  • 8
  • 3
    The "red.png" in the same directory as "GameBoard.java" file? It is advisable to name the variables **do not start with uppercase** letters! – adampweb Aug 02 '16 at 11:02
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) Load the icon at start-up or initialisation and simply set it to the button when needed. .. – Andrew Thompson Aug 02 '16 at 11:09
  • 1
    .. 5) Instead of `GameBoard.class.getResource("red.png")` I would recommend `this.getClass().getResource("red.png")` 6) Are you ***sure*** the name is `red.png` as opposed to `Red.png` or `red.PNG`? Using `getResource(..)`, case matters. – Andrew Thompson Aug 02 '16 at 11:09
  • 1
    and another unnecessary coding. `if (Play == true) ` since Play is boolean you don't need to check whether it is true. – sushi Aug 02 '16 at 11:10

0 Answers0