This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Resizing a picture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resizing a picture" Watch "Resizing a picture" New topic
Author

Resizing a picture

Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
I am trying to resize a picture and scrollpane when a user imports an image into the program. However, when I call the setSize methods, the size of the image remains unchanged. I've tried using maximum size and minimum size, as well as calling revalidate methods, but the size of the picture remains the default size when the program begins. What am I doing wrong here? (Also, this isn't the finished code. I am simply concerned with getting these sizes to change at runtime.)

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Images are painted at there actual size unless you do custom painting. You have two options.

a) if you just want to scale the image to a different fixed size then you can use Image.getScaledImage(...);

b) if you want the image to dynamically resize as you resize the frame then you need to do custom painting of the image. The Background Panel gives you an option to do this.
Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
Ok, thank you for your time. I will work with image resizing and find out if it suits my design.
Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
Ok, I almost have it, but I am confused with working with the Graphics class. I have a resized image which I used through some examples online, as well as a canvas object which can paint that image. Trying to understand this more, but I keep getting a blank page. What else could I be doing wrong here? That is, how do I make it fire the paint() method? I have a line of code to tell me when it fires, but nothing seems to be updating...

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
1) There is not need to create a custom class to display the image. Just add the resized image to a JLabel

2) Even if you did need a custom class you would NOT extend Canvas. Canvas is an AWT component and your original code was using an ImageIcon which indicates you are using Swing. Did you even look at my example?
Chris Swanson
Greenhorn

Joined: Nov 30, 2009
Posts: 15
Ah, I forgot I could create a new Icon using the image that was resized. I was thinking the icon had to be read from a file for some reason. Thank you very much for your help!!
 
GeeCON Prague 2014
 
subject: Resizing a picture