1

Im trying to make a simple board game in which when you click a button its image is set to that of the character you have selected.

This:

board[x][y].setIcon(Soldier.iconImg);

Does not work. It places a tiny white dot on the button and nothing else. The Soldier.iconImg calls for an Icon from the Soldier class.

String name = soldier;
Icon iconImg = new ImageIcon("/Resources/"+name+"Img.png");

Now when i do this:

board[x][y].setIcon(new ImageIcon("/Resources/soldierImg.png"));

It works fine.

I've found a bit of a way around the problem:

board[x][y].setIcon(new ImageIcon("Resources/"+Soldier.name+"Img.png"));

I am however still quite confused as to why the first method doesn't work while the second does. Anybody have any idea?

  • 1
    With the code you posted, there is no way to know what the value of the `soldier` variable is. All we know is that it's not "soldier". And that you don't respect Java naming conventions. And that you're using public fields. If you want help,post a complete minimal example reproducing the problem. – JB Nizet Oct 31 '15 at 07:04
  • String name = "soldier" NOT String name = soldier; – Mehdi Oct 31 '15 at 08:12

0 Answers0