• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make background a picture?  RSS feed

 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Everyone,
I want to make a picture as my panel's background. How can I achieve it?
Thanks a lot.

Have a nice day
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a demo. You can set the layout and add your components either inside the (BackgroundPicturePanel) class or with the reference when you instantiate the class

If you use this as a content pane (f.setContentPane(bpp)) be sure to set it opaque (bpp.setOpaque(true) or setOpaque(true) inside class constructor) to avoid problems with LookAndFeels that may use non–opaque JPanels.
 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Craig, but I modify my code exactly as your code do. But when I run the program, JBuilder report me a exception as follows:
java.lang.IllegalArgumentException: input == null!


at javax.imageio.ImageIO.read(ImageIO.java:1338)

at gui.PointPanel.loadImage(MainFrame.java:222)

at gui.PointPanel.<init>(MainFrame.java:161)

at gui.MainFrame.jbInit(MainFrame.java:64)

at gui.MainFrame.<init>(MainFrame.java:51)

at gui.ElecCtrl.<init>(ElecCtrl.java:20)

at gui.ElecCtrl.main(ElecCtrl.java:50)


I check the directory name, the file name,everything that may cause the iamge loading failure, but it seems didn't work...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method will look for the image in your classpath

If the image is not located in your classpath you can try making a new URL

to a file on your hard drive. path to photo above could be something like "C:/My Documents/My Pictures/images/baldeagle.jpg".
 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see... Thanks for ur help
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!