Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image

 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you put an image into a JPanel??? i can't do ".add(creatureImage, BorderLayout.CENTER);" ��??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
[ June 07, 2006: Message edited by: marc weber ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the simple way is
panel.add(new JLabel(new ImageIcon("creature.gif")));

and will appear depending on layoutManager

if you want a background image (from the faq)
http://faq.javaranch.com/view?BackgroundImageOnJPanel
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
were do i tell it were to get the picture from ...? i have it saved in my pictures on my computer so how do i tell it to take it from there? do i just include it in name?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the image is in a sub-directory e.g. 'images'
panel.add(new JLabel(new ImageIcon("images/creature.gif")));

or you can specify the full path
panel.add(new JLabel(new ImageIcon("c:/java/images/creature.gif")));

note: if you want to use \, you'll need to use \\
panel.add(new JLabel(new ImageIcon("c:\\java\\images\\creature.gif")));
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the picture have to be in "gif" format?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> does the picture have to be in "gif" format?

can also be .jpg or .png

I seem to recall other formats being supported in the newer versions of java,
but not sure what they are.
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok cuz the picture that i have that i want to load or bitmaps. and when i add the code to write then they don't appear.


also i am running java 1.5
[ June 12, 2006: Message edited by: chris barr ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can load .bmp images with the ImageIO class and hand the returned
BufferedImage to your ImageIcon constructor. ImageIO support for .bmp
and .wbmp images starts in j2se 1.5

edit for width
[ June 13, 2006: Message edited by: Craig Wood ]
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------------- <--Explanation


<---- my head



the explanation isn't really above my head i understand it i just don't know how to us said methods...... while i wait for a reply i will mess around and try to figure it out..... thanks
[ June 12, 2006: Message edited by: chris barr ]
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok what i am trying to do now is ...



that doesn't work .. but it compiles and runs and doesn't give an error and i am not catching it or thowing anything.??? let me know if you want more code.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It takes some experimenting, like this:

If you would rather stick with the ImageIcon class for loading, check the
ImageIcon api for the "getImageLoadStatus" which will return information
about loading success.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick thank you - this has solved a problem I was struggling with, I'm using:



Works perfectly!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic