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 How to put image on 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 "How to put image on JPanel" Watch "How to put image on JPanel" New topic
Author

How to put image on JPanel

Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
Hy

Can you tell me how to put an image to JPanel.

I tried this but it don't work



btw raptor.jpeg is in C:raptor.jpeg and the .java is in C:/users/accountName/eclipse/picture/src/picture.java
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

Your code looks for the image in the current working directory. Try specifying the full path to the image.

And don't read the image in the paintComponent method. That method is called very frequently. Read the image in your initialization code and just draw it in the paintComponent method.
Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
Can you tell me which path sould I write? I tried everything that comes on my mind but still without sucess.
As I mentioed before the picture is here C:/raptor.jpeg and the .java is in C:/users/accountName/eclipse/picture/src/picture.java
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

new ImageIcon(//point to the image you want to load)

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

Filip Stirn wrote:Can you tell me which path sould I write? I tried everything that comes on my mind but still without sucess.
As I mentioed before the picture is here C:/raptor.jpeg and the .java is in C:/users/accountName/eclipse/picture/src/picture.java


How is it that you didn't try "C:/raptor.jpeg" then?

Or perhaps your problem is not that the image isn't found, but something else. I did notice that you didn't describe your problem at all.
Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
Paul Clapham wrote:
Filip Stirn wrote:Can you tell me which path sould I write? I tried everything that comes on my mind but still without sucess.
As I mentioed before the picture is here C:/raptor.jpeg and the .java is in C:/users/accountName/eclipse/picture/src/picture.java


How is it that you didn't try "C:/raptor.jpeg" then?

Or perhaps your problem is not that the image isn't found, but something else. I did notice that you didn't describe your problem at all.


I tried that but still when I launch programm it just shows JFrame with JPanel but without picture
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
I think this discussion would sit better on the GUIs forum. Moving.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

I don't see where you set the preferred size of your DrawPanel, so perhaps it's zero. You can test where a component is and how big it is by putting a Border around it. A LineBorder would do perfectly well.
Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
It still don't work. Can someone just please try my code, fill in what is wrong or missing and then paste the working code here?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

Did you move the image reading part out of the paint component like Paul said? Can you show us your latest code?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

Filip Stirn wrote:It still don't work. Can someone just please try my code, fill in what is wrong or missing and then paste the working code here?


Nope. We aren't here to do your work for you.

So tell us, what exactly did you see when you drew a thick red border around your panel?
Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
My code:


I don't know how to set border around picture. And the only thing I see when i launch this program is JFrame.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Setting the size of your panel does nothing - the frame's content pane's BorderLayout ignores it. You should set the preferred size instead, as that's being used by most layout managers.

As for the border, you can always give your panel itself a border, using its setBorder method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Filip Stirn
Greenhorn

Joined: Aug 18, 2011
Posts: 8
Rob Spoor wrote:Setting the size of your panel does nothing - the frame's content pane's BorderLayout ignores it. You should set the preferred size instead, as that's being used by most layout managers.

As for the border, you can always give your panel itself a border, using its setBorder method.




I guess I should give up cause I'm just to dumb for this
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i dont know if this will help but:
first, i would copy or move the image file to the same folder as the java code. then you can just use its name.
second, i cant remember why but i learned or was taught that the first line of code in your paintComponent method should be super.paintComponent():


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

After adding the super.paintComponent(g) as Randall suggested, that code works for me. So what's it not doing that you think it should?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to put image on JPanel