louisgnarf
Programmer
What would be the best way to set the background of a JPanel as an image?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
class ImagePanel extends JPanel
{
private Image image;
public void paintComponent(Graphics g)
{
//Now draw the image scaled.
Rectangle r = this.getBounds();
int scaleWidth = r.width;
int scaleHeight = r.height;
String imagePath= "/com/ario/gui/resourceLibrary/images/wizardBG.jpg";
ImageIcon icon = new ImageIcon(getClass().getResource(imagePath));
image = icon.getImage();
g.drawImage(image, 0, 0, scaleWidth, scaleHeight, this);
}
}