aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image" Watch "Image" New topic
Author

Image

chris barr
Ranch Hand

Joined: Apr 20, 2006
Posts: 37
how do you put an image into a JPanel??? i can't do ".add(creatureImage, BorderLayout.CENTER);" ��??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Moving to the Swing forum.
[ June 07, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 20, 2006
Posts: 37
thanks
chris barr
Ranch Hand

Joined: Apr 20, 2006
Posts: 37
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 20, 2006
Posts: 37
does the picture have to be in "gif" format?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Apr 20, 2006
Posts: 37
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Apr 20, 2006
Posts: 37
------------- <--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

Joined: Apr 20, 2006
Posts: 37
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Jan 31, 2006
Posts: 129
Just a quick thank you - this has solved a problem I was struggling with, I'm using:



Works perfectly!
 
Consider Paul's rocket mass heater.
 
subject: Image