aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Resize JFrame to fit image 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 "Resize JFrame to fit image" Watch "Resize JFrame to fit image" New topic
Author

Resize JFrame to fit image

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Just got a simple program to display an image in a window. This image is displayed but the JFrame pops up with minimal dimensions.

I could experiment to find the correct size and use something like , but how do I get it to find the correct size of whatever image I use automatically?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

The Image class provides you methods to determine the width/height which you can use to resize your frame.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
There is no need to do custom painting. If you do need to do custom painting for some reason, you should never read the image in the paintComponent() method. This method is called many time when Swing determines a component needs to be repainted.

Add the image to a JLabel and add the label to the frame.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I should perhaps add that I have tried many combinations of image.getWidth() etc and setting the preferred size of the JPanel and setting the JFrame to this size and none of them work.

Maneesh, could you be a bit more specific?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Rob Camick wrote:There is no need to do custom painting. If you do need to do custom painting for some reason, you should never read the image in the paintComponent() method. This method is called many time when Swing determines a component needs to be repainted.

Add the image to a JLabel and add the label to the frame.




does the job. Thanks.

I'm still curious how I would do this if I did have a panel with a custom paintComponent method that I wanted a frame to fit around.
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

Just for kicks, try


SCJD 6, OCPJP7, Baroque Potion, G+
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

David, this doesn't work because image is not in scope in the main method.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Rob Camick wrote:If you do need to do custom painting for some reason, you should never read the image in the paintComponent() method. This method is called many time when Swing determines a component needs to be repainted.


Surprised, I thought overriding the paintComponent() method was the standard way to put graphics in a JPanel? I'm following the example on p 365 of Head First Java, which is where I got this from.

Say I have a slightly more complex example where I want to paint a red spot on my image - I'd need to override paintComponent() then? In this case, how do I get my JFrame to fit my JPanel?



Which produces this:


which expands to:
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
I already said you should NEVER read the image in the paint component method.

Yes, custom painting is done in the paintComponent() method of a component. But there are many different approaches.

1) You could override a JLabel. Then you just imvoke super.paintComponent() and then add your custom painting code.

2) If you want to draw the image and the spot yourself, then you still override the paintComponent() method. But in addition you would need to override the getPreferredSize() method to return the dimension of the image, so the layout managers know how to layout your custom component.

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I think I misunderstood you before - I thought you were saying I shouldn't use an image in a paintComponent method at all, but if I understand correctly, you're saying that I shouldn't read it in in that method - to save on disk I/O...?

I changed my code to



And this does what I want it to do... thanks for the pointers. But overriding JLabel is better in your book?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Another way I just tried that works:

Add a constructor and set the preferred size there, so you don't need to override the getPreferredSize() method. (I thought I'd tried this before, but I was probably doing something dumb like setting it in the paintComponent() method, which isn't called until after the frame has been packed.)

Just wondering, does the preferred size get set automatically when other components are added? Which might imply that using a constructor is not a good solution, because it might get changed if we add more items to the panel?


On a side note I also found that using setSize(...) on the JFrame isn't a very good idea unless you know to take account of the borders by using the getInset() method (a bit fiddly), because the edges of the picture get cut off.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resize JFrame to fit image