File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Background image in JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background image in JPanel" Watch "Background image in JPanel" New topic
Author

Background image in JPanel

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
I'm using eclipse 3, and at a given point i've this code - that was suposed to display a background image (yes the jpg does exist at given path):

if i use the gradient everything goes fine, but when i chose to use the jpg it does not show up
What am i doing wrong?

TiA


java amateur
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
worked OK for me, but changed to a .gif file in local folder
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks, Michael!

so error must be in image path: i created a folder called img that lies inside bin directory: is that correct?

(i confess i tried every single possible location for my jpg and also tried with gif

TiA
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> new ImageIcon("img/mar.jpg").getImage();

java will be looking to find mar.jpg in folder img which is a subfolder
of the current folder
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
by current folder you mean bin (classes) or src?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the folder you are running your program from

eg
your current java file is ImageBackground.java
and it is in the folder
c:\Documents and Settings\ML\Java\ImageBackground

java will look for mar.jpg in
c:\Documents and settings\ML\Java\ImageBackground\img

it may also look in bin, ext, and classpath folders,
but I'm not 100% sure about that - someone else may
be able to confirm.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thank you again, Michael!

i'm really puzzled with this
as i said earlier, i tried copy/paste my img folder into every reasonable location
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's the full program I'm working with.

the image shows up both with and without the gradient code.
the image also shows up, as above, when the .gif file
is in the same folder, or in a subfolder

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks again for your continuous interest, Michael!

i got the solution from here

as to path, i have my gui code inside bin, in package called gui; inside bin, too, i have a folder called img (with my images): this explains my new path

here's my (working) code:


this code:
doesnt work (but dont ask me why)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Background image in JPanel