Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background image for a JPanel

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
What would be the best way to set the background of a JPanel as an image?
 
I found this code in another forum:

Code:
     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);
            }
      }

This assumes the dimensions of the image are the same as the dimensions of the panel. You may have to screw around with scaleWidth and scaleHeight to achieve what you want.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top