0

I'm a java beginner, and my java skills suck on this. In my jframe, i have this jpanel(below) which draws the image. I want the image changed whenever a radiobutton(in another frame) fires an ItemEvent and click something like a save button that'll fire an ActionEvent. i intend this to be of use for a character-selecting interface, as in MMORPGs, etc. Image panel below is intended for weapon-selecting interface in my game.

class Weapons extends JPanel {

    private Image weaponimage = weapon2.getImage();

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);

        g.drawImage(weaponimage, 0, 0,  this);
    }
}

weapon2 is an ImageIcon. thanks in advance.

FartVader
  • 143
  • 1
  • 3
  • 13

1 Answers1

3

The simplest approach is not to have a custom panel at all, but use JLabel as the image container. Then you can change the image with:

label.setIcon(new ImageIcon(theNewWeaponImage));

Edit: since it looks like you already have Icons, you can use those directly:

label.setIcon(theNewWeaponIcon);
kiheru
  • 6,588
  • 25
  • 31
  • thanks. but i quite have a purpose why i made it this way. the problem's actually that i don't know how to declare an Image image from outside this custom panel class, and if i do i'd be then able to let if statements do the rest for me. but thanks anyway sir. – FartVader Oct 17 '13 at 13:23
  • @RejAverion how do you intend to change the image from *anywhere* if you have no way to determine what the image should be? Looking a bit closer at the question, it seems that you have `Icons`. Then `label.setIcon(theNewIcon)` should work. (added that to the answer) – kiheru Oct 17 '13 at 13:32