• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in setting background image of jpanel

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m not able to set a background image of my JPanel, can anyone tell me how do i set JPanel Background image??
here is my code, in which i m setting the background color of my JPanel but i want to set an image instead:


 
Paul Clapham
Sheriff
Pie
Posts: 20721
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:i m not able to set a background image of my JPanel, can anyone tell me how do i set JPanel Background image??


Yes, I'm sure many people can. I used jpanel background image as my Google keywords and that brought up quite a list of those people. Give it a try (searching the web I mean).
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an example is also in the Swing FAQ (main page of Swing forum)
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i just use the code which is in swing faq, but not working for me...
what wrong i m doing..?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as I explained in one of your other topics, it has no size - you have to set it.

if you want a background image for Cpoint, then the code in BackgroundPanel
should be part of Cpoint, not adding another panel to it
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if you want a background image for Cpoint, then the code in BackgroundPanel
should be part of Cpoint, not adding another panel to it




But still not working...
 
Paul Clapham
Sheriff
Pie
Posts: 20721
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is all wrong:


First of all getResource() already returns a URL, so wrapping it in a URL seems pointless. And using the URL constructor which gets a relative URL for a resource with the same name also seems pointless. But the real problem is that getResource doesn't take a file path in the first place. If you want to use a file path, a File object is the thing to use. The getResource() method is for finding resources in the classpath. So use a File object.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, like this...


and i m getting exception in this line

 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like you're trying to add point to itself using a new BackgroundPanel as constraints. That's obviously not going to work.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what to do here, can you correct me??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from your last posted code, this part is so wrong it's not funny



you really need to read Sun/Oracle's painting tutorial,
because you need to discover why the above is wrong.
no point just being told why.

http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i will..
one more thing i really want to know always, and i believe you can help me in that also,
which is the best way to read javadoc??
because javadoc always provides only classes,methods,constructor etc.
and most of times i don't know how to use most of them even..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the info (limited) in the javadocs is at the top.

this link is the best for 'How To ...'

http://docs.oracle.com/javase/tutorial/uiswing/TOC.html
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thank you very much for this..
one more thing these table of contents are for swing,
where do i get this for java core..?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:okay thank you very much for this..
one more thing these table of contents are for swing,
where do i get this for java core..?


Did you try clicking links on that page to see where you end up?
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try clicking links on that page to see where you end up?

no i just looked at the TOC.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then please ShowSomeEffort and read the page itself.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i corrected my paintComponent, but still it's not working, here it is

 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never load any resources in any painting method. paintComponent may be called multiple times per second, and that will slow down painting significantly. You should instead load the image in the constructor and keep a reference to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic