• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

RE: image ico

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/BackgroundImageOnJPanel
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic