• Post Reply Bookmark Topic Watch Topic
  • New Topic

RE: image ico

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to add a image as a background on a panel i tried it by using ImageIcon icon = new ImageIcon("box.jpg");
JLabel iconl = new JLabel(icon);
but the panel consists of other components also like textfields. I am getting the image below the textfields when i added the JLabel. I actually want this image to be set as the complete background on which other components must be placed. Some body please help me to solve this.

Thanks & Regards
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
 
ashok kandaswamy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.i am only suggestion is instead of setting icon to Jlabel.make it one Jpanel and setback round as image..then add whatever we ant to add on this Jpanel.

like

leftPanel = new ImagePanel(imgPath + "final/left_bg.jpg"); //here path of he image

public class ImagePanel extends JPanel{
private Image img;

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

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

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

}



Originally posted by adeeb alexander:
Hi,
I want to add a image as a background on a panel i tried it by using ImageIcon icon = new ImageIcon("box.jpg");
JLabel iconl = new JLabel(icon);
but the panel consists of other components also like textfields. I am getting the image below the textfields when i added the JLabel. I actually want this image to be set as the complete background on which other components must be placed. Some body please help me to solve this.

Thanks & Regards
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do a lot with the painting. The code in Gregg's link and Ashok's post will scale (stretch) it to cover the entire panel. With some effort you can draw it any way you want, including tiled.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!