• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in setting background image of jpanel

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Marshal
Posts: 28193
95
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

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).
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an example is also in the Swing FAQ (main page of Swing forum)
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
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
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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, like this...


and i m getting exception in this line

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then please ShowSomeEffort and read the page itself.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay i corrected my paintComponent, but still it's not working, here it is

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic