Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Background image in JPanel

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
worked OK for me, but changed to a .gif file in local folder
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by current folder you mean bin (classes) or src?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic