Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to put image on JPanel

 
Filip Stirn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new ImageIcon(//point to the image you want to load)
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would sit better on the GUIs forum. Moving.
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you move the image reading part out of the paint component like Paul said? Can you show us your latest code?
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Filip Stirn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4363
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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():
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic