• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

setting background image on jframe

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to set background image for jframe but was not able to do so .Can someone explain mistake in my code.


 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, UseCodeTags (this is a link) when posting. I have added them for you this time. Doesn't it look better?

You can try replacingwith
The reason is, the image has not beed loaded when drawImage() is called for the first time. You can verity this by printing the result of this method (it returns false if the image has not been loaded, true otherwise).
Adding the panel as ImageObserver ensures that the image will be drawn when fully loaded.

Also, don't write String args[]. It should be String[] args.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Your code would have looked even better with indentation; we have some suggestions here.
I am 99.9% sure that writing .* after Toolkit in the import declaration was a mistake. What do you actually want to import?
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to put a image on jpanel but i was gettong a zoomed in image.Can someone tell how to get the actual image as background.
screenshot of output is attached.

original image
Screenshot-from-2015-10-10-20-47-32.png
[Thumbnail for Screenshot-from-2015-10-10-20-47-32.png]
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I shall merge your thread with your other one because they are about related subjects.

I am 99% sure that the problem has to do with the size of the image. If you open that image with a photo editor, it will probably say it is 8MB size and 3450×2300px. Now that is larger than most screens. Unlike a photo editor, a Swing component does not zoom automatically. Start by shrinking that image with your photo editor, in which case it will probably occupy little enough space that the whole picture is visible. Not sure what to do to about zooming. You can try adding the panel to a scroll pane. You can try copying the Graphics object with its create() method, casting to Graphics2D and using its scale method. But I am not quite sure how you would code that,

You should also always run Swing code on the Event Dispatch Thread.
 
Rancher
Posts: 976
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your answer has already been stated, but as I dislike imageIcon, here is another way of doing it:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic