• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to put image on JPanel

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new ImageIcon(//point to the image you want to load)
 
Paul Clapham
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80869
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this discussion would sit better on the GUIs forum. Moving.
 
Paul Clapham
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic