File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent help with adding an image to jframe

 
john mattucci
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I`m trying to add a background to a JFrame as a wallpaper the problem im having is that
the image appears in the centre of the frame and I cant seem to figure out how to make it
cover(the image) the entire frame and for the image to be able to resize if i were to
maximize or minimize the frame
background = new JLabel(new ImageIcon("c:\\windows\\desktop\\bg0063.jpg"));
getContentPane().add(background)
where do I go from here
 
Grant Crofton
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used SWING, but it looks like the problems you get are because of how you draw the image - it's part of a JLabel, which gets put in the centre of the frame.
You could try setting the frame's layout to a FlowLayout, so it gets put at the start, with something like

NOTE: I'm guessing that's how you use getContentPane(), but I've not used it, so somebody pls correct me if I'm wrong.
You might be better off drawing the image yourself in the JFrame's paint().
It would go something like this:

I haven't tested that code, but I think it's right.
Then you can go into scaling the image according to the size of the frame, etc.
One thing you might get with that is the frame title & sides cover some of the Image - if this happens, use

in your paint().
Hope that helps.
 
gautham kasinath
Ranch Hand
Posts: 583
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well have u tried to place the JLabel with the start position and the width and height with setBounds(int x, int y, int width, int height); .. well I guess that should help.
ne way lemme know if that works.
If it doesnt I shall try n find a work around for u r problem..
All the best
Regds
Gautham Kasinath
 
Frank Hale
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a code snippet at
http://codeguru.earthweb.com/java/articles/179.shtml
which shows how to add a background image to a JTree. It tiles it nicely like you want. I wonder if there is something in that code you could use to do the same for your JFrame.
Hope this helps!
Frank
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic