aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Stretch the image size. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Stretch the image size." Watch "Stretch the image size." New topic
Author

Stretch the image size.

Ashish Gundewad
Greenhorn

Joined: Oct 10, 2010
Posts: 14
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

The Graphics class has an overloaded drawImage() method which allows you to specify the image width and height.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ashish Gundewad
Greenhorn

Joined: Oct 10, 2010
Posts: 14
Yes sir,
I get it, but can you tell me how it is done. Because, i am new to java.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

How are you creating your frame with a background image?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

This is how it's done
http://www.coderanch.com/how-to/java/BackgroundImageOnJPanel
Ashish Gundewad
Greenhorn

Joined: Oct 10, 2010
Posts: 14
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Oct 10, 2010
Posts: 14
Here is the code:
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Oct 10, 2010
Posts: 14
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Oct 10, 2010
Posts: 14
Ok, i will try it.

Sir, Have you executed the above code?
Ashish Gundewad
Greenhorn

Joined: Oct 10, 2010
Posts: 14
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Oct 10, 2010
Posts: 14
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.!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stretch the image size.