Malii
Malii

Reputation: 448

Issues with ImageIcon and images on buttons - JFrame

this has been driving me crazy all day so I figured I'd post it here and see if someone else can work it out. First off I tried to add a background image, the default ImageIcon was not working so I went with overriding the paint method instead. #

public class ImageJPanel extends JPanel { 
private static final long serialVersionUID = -1846386687175836809L;
Image image = null; 

public ImageJPanel(){ 
    addComponentListener(new ComponentAdapter() { 
    public void componentResized(ComponentEvent e) { 
        ImageJPanel.this.repaint(); 
    } 
}); 
}
//Set the image.
public ImageJPanel(Image i) { 
  image=i; 
  setOpaque(false); 
} 
//Overide the paint component.
public void paint(Graphics g) { 
  if (image!=null) g.drawImage(image, 0, 0, null); 
  super.paint(g); 
} 

}

Once I used that it worked fine, however now I want to add images to my buttons but it is not working. Here is how my buttons work:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Images images = new Images();
JPanel j = new ImageJPanel(images.menuBackground); 
j.setLayout(null);
JButton Button_1= new JButton("Button_1",new ImageIcon("images/gui/Button.png"));

Insets insets = j.getInsets();
Dimension size = Button_1.getPreferredSize();
Button_1.setBounds(300 + insets.left, 150+ insets.top, size.width, size.height);

Singleplayer.setSize(200, 50);

j.add(Button_1);

frame.add(j);
frame.setSize(800,600);
frame.setResizable(false);
Button_1.addMouseListener(singleplayerPressed);

frame.setVisible(true);

All my images are .png, could that affect it?

Upvotes: 0

Views: 666

Answers (2)

tenorsax
tenorsax

Reputation: 21223

Try this:

ImageIcon image = new ImageIcon(this.getClass()
                .getResource("images/gui/Button.png"));

Side note, you should override paintComponent() not paint(). Also make sure to call super implementation before doing all your painting. For more details see Lesson: Performing Custom Painting tutorial.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347184

Let's start with this:

public void paint(Graphics g) { 
    if (image!=null) g.drawImage(image, 0, 0, null); 
    super.paint(g); 
} 

This is the wrong approach. Firstly, you really don't want to override the paint method UNLESS you absolutely know that this is the correct approach to your problem (and without knowing more, I'd suggest it isn't).

Secondly, you paint your image on the component, then promptly paint over the top of it...(super.paint(g); can have the ability to paint over your work, I know the panel is opaque, but this is still a very bad approach).

Use paintComponent instead

protected void paintComponent(Graphics g) { 
    super.paint(g); 
    if (image!=null) g.drawImage(image, 0, 0, null); 
} 

PNG images are fine, they are supported by Swing out of the box.

Make sure that your program can see the image. Is it been loaded from the file source or is it a resource within you JAR?

Try this:

System.out.println(new File("images/gui/Button.png").exits());

If your program can see the file, it will return true other wise the program can not see the file and that is your problem.

Upvotes: 2

Related Questions