• 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

Stretch the image size.

 
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir,
I am creating a JFrame with background image. My JFrame is extended in full size of my screen by using setExtendedState() method,
but when i place an image in the Frame , it appears small related to the Frame.
Please help me, is there any way to stretch the image to the size of the JFrame.

Thanks.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Graphics class has an overloaded drawImage() method which allows you to specify the image width and height.
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes sir,
I get it, but can you tell me how it is done. Because, i am new to java.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you creating your frame with a background image?
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how it's done
https://coderanch.com/how-to/java/BackgroundImageOnJPanel
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sir, its working now.
But now im facing a new problem that i have placed some components on a JPanel
and im not able to see those components except TextFields.
It seems that the image is overlapping the components.
The layout is BorderLayout.CENTER, but the TextFields appears on the top of the JFrame.

How can I get those components Visible and at the center of the screen.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your sscce code which demonstrates the problem. Difficult to say whats going wrong without looking at the code
PS. Please do take out time to click the link and find out what sscce really means.
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code:
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In future, while posting code, please UseCodeTags
I have edited your original post. As you can see, the code tags makes the posted code easier to understand.



What do you think is happening here?
Hint 1: Any JFrame instance can have only one content pane, which defaults to BorderLayout.
Hint 2: JFrame also exposes a setContentPane() method
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry sir about the code tags, i was not knowing about it.

You mean to say that JFrame is getting confused for his default content pane.
Am I right?

So what should i do then?
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Tell the frame to use your content pane using the setContentPane()
2) Add your components using the getContentPane().add()

PS. Your ContentPane extends JPanel which defaults to flow layout. You will need to set the layout to BorderLayout if you want to add components to BorderLayout.NORTH
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i will try it.

Sir, Have you executed the above code?
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir, im getting confused.
I did the way you said. But, nothing happened.

Can you tell me how to do this in simple way.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you got the code ready made, you might as well find out and let us know
1) Why does the code use the SwingUtilities#invokeLater
2) Why does the code use getClass().getResource()
 
Ashish Gundewad
Greenhorn
Posts: 14
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, now there is no problem. thanks sir.

1) I think that SwingUtilities is a class and invokeLater is its method. i have not much idea about the class, its is simply related to Threading.
But invokeLater() method is used it schedule the task in the Thread.

2) getClass() is used to get Image and also used to get information about the Image.
getResource() checks the image in the directory with the help of Class Loader, it returns the URL of the Image.

Hope I am right.!!
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic