• Post Reply Bookmark Topic Watch Topic
  • New Topic

setting background image on jframe

 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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.


 
Paweł Baczyński
Saloon Keeper
Posts: 1955
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • 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: 52581
119
  • Mark post as helpful
  • send pies
  • 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
  • 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: 52581
119
  • Mark post as helpful
  • send pies
  • 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.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer has already been stated, but as I dislike imageIcon, here is another way of doing it:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!