This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes problem in setting background image of jpanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in setting background image of jpanel" Watch "problem in setting background image of jpanel" New topic
Author

problem in setting background image of jpanel

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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

Joined: Jun 09, 2003
Posts: 4632
an example is also in the Swing FAQ (main page of Swing forum)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

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

Joined: Aug 20, 2011
Posts: 1000
    
    2

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
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay, like this...


and i m getting exception in this line

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

That looks like you're trying to add point to itself using a new BackgroundPanel as constraints. That's obviously not going to work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
what to do here, can you correct me??
Michael Dunn
Ranch Hand

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: May 03, 2008
Posts: 4642
    
    5

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?


luck, db
There are no new questions, but there may be new answers.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Did you try clicking links on that page to see where you end up?

no i just looked at the TOC.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Then please ShowSomeEffort and read the page itself.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay i corrected my paintComponent, but still it's not working, here it is

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in setting background image of jpanel