0

I would like to add a jlabel to a jpanel I've used this code

JLabel background=new JLabel(new ImageIcon("Myimage.jpg"));
panel.add(background);
frame.setContentPane(panel);

the image is sized with the minimized window not the maximized one, so when I maximize the window it shows an empty space which I don't want. I would like a background for the jpanel I added

Lose' CKi
  • 31
  • 6
  • You’ve described what you don’t want; now, tell us what you do want. What should be seen when the window is maximized? Do you want the image stretched? Centered? Tiled (that is, repeated)? – VGR Apr 13 '16 at 17:28
  • stretched with the maximized window to fill it, its a background. – Lose' CKi Apr 13 '16 at 17:34
  • Since you're new here, please don't forget to mark the answer accepted whenever it helped (most) in solving the problem. See also http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – Paul Wasilewski Apr 13 '16 at 18:34

1 Answers1

0

Just override the paintComponent(Graphics g) : void of your JPanel. The following solution will scale your image always to the panel size.

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

    private Image image;

    public void setBackgroundImage(Image image) {
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(this.image, 0, 0, this.getWidth(), this.getHeight(), this);
    }
}

For performance and quality aspects it could be also useful to set some RenderingHints.For more information see Controlling Rendering Quality.

Please consider to search first before you ask. Maybe this answer adresses your problem How to fit Image size to JFrame Size? as well?

Community
  • 1
  • 1
Paul Wasilewski
  • 9,762
  • 5
  • 45
  • 49
  • 1
    The final argument to the drawImage method should be `this`. – VGR Apr 13 '16 at 17:38
  • should I use a buffered image or should I use the jlabel? – Lose' CKi Apr 13 '16 at 17:39
  • The direct way would be to use a `BufferedImage`. But `new ImageIcon('xyz').getImage();` is also possible. – Paul Wasilewski Apr 13 '16 at 17:45
  • 2
    `g.drawImage(this.image, 0, 0, this.getWidth(), this.getHeight(), null);` should be `g.drawImage(this.image, 0, 0, this.getWidth(), this.getHeight(), this);`, especially when using `Image` as the actual image data might not have been realised when it was passed to you and when you tried to paint it. Also, you should override the `getPreferredSize` method and return the size of the image at least – MadProgrammer Apr 13 '16 at 22:40
  • 1
    @Lose'CKi `JLabel` will work, but if the required size of the components within ite exceed the size of the image, they will be clipped. `ImageIO.read` is more preferrable then `ImageIcon` for a number of reasons, but manily because it throws an `IOException` when the image can't be loaded instead of failing siliently. `BufferedImage` extends from `Image` so you can use it whereever `Image` can be used – MadProgrammer Apr 13 '16 at 22:42