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?