Setting our own background image for a JPanel...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mir_Taqeeulla
Posted On:   Monday, April 21, 2008 07:27 AM

For a JPanel we can set the background Color by using
setBackground(Color.XXXX) method. But these colors are
not that attractive. How can i set my own background
(similar to as we do in HTML) with an image.

If someone has got the code for that, can please help with that...

Thanks in Advance...

Re: Setting our own background image for a JPanel...

Posted By:   Anonymous  
Posted On:   Monday, April 21, 2008 09:14 AM

Here an example:

class ImagePanel extends JPanel {

private Image image;

public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img) {
this.image= image;
Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}

}
About | Sitemap | Contact