File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resize JFrame to fit image

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10976
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Image class provides you methods to determine the width/height which you can use to resize your frame.
 
Rob Camick
Ranch Hand
Posts: 2476
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
Clojure Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for kicks, try
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, this doesn't work because image is not in scope in the main method.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2476
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic