ximotur
asked on
Resize an ImageIcon
Hiya,
I am doing an application where I have to take a picture from Internet and use it like an ImageIcon inside a JLabel in a GridLayout, this picture can have different sizes and If it is too big the cells from the GridLayout will be too big so I manage to resize the ImageIcon using this code:
private ImageIcon resizeImage( ImageIcon fromStream,
int widthConstraint, int heightConstraint)
{
int imgWidth = fromStream.getIconWidth();
int imgHeight = fromStream.getIconHeight() ;
ImageIcon adjustedImg;
if ( imgWidth > widthConstraint | imgHeight >
heightConstraint )
{
if ( imgWidth > imgHeight )
{
// Create a resizing ratio.
double ratio = (double) imgWidth / (double)
widthConstraint;
int newHeight = (int) ( (double) imgHeight / ratio );
// use Image.getScaledInstance( w, h,
// constant), where constant is a constant
// pulled from the Image class indicating how
// process the image; smooth image, fast
// processing, etc.
adjustedImg = new ImageIcon(
fromStream.getImage().getS caledInsta nce(
widthConstraint, newHeight,
Image.SCALE_SMOOTH )
);
}
else
{
// Create a resizing ratio.
double ratio = (double) imgHeight / (double)
heightConstraint;
int newWidth = (int) ( (double) imgWidth / ratio );
adjustedImg = new ImageIcon(
fromStream.getImage().getS caledInsta nce( newWidth,
heightConstraint, Image.SCALE_SMOOTH )
);
}
// return the adjusted ImageIcon object.
return adjustedImg;
}
else
{
// Assure the resources from the adjustedImg object
// are released and then return the original ImageIcon
// object if the submitted image's width and height
// already fell within the given constraints.
adjustedImg = null;
return fromStream;
}
}
**********
The problem is that the when I add the Jlabel in the panel the it uses the same space as it wasn't resized, is like the function doesn't work but checking the values of the imageIcon with getIconHeight() the values are correct, so why when I add the Jlabel in the panel the sized is not changed??
thank you!!
I am doing an application where I have to take a picture from Internet and use it like an ImageIcon inside a JLabel in a GridLayout, this picture can have different sizes and If it is too big the cells from the GridLayout will be too big so I manage to resize the ImageIcon using this code:
private ImageIcon resizeImage( ImageIcon fromStream,
int widthConstraint, int heightConstraint)
{
int imgWidth = fromStream.getIconWidth();
int imgHeight = fromStream.getIconHeight()
ImageIcon adjustedImg;
if ( imgWidth > widthConstraint | imgHeight >
heightConstraint )
{
if ( imgWidth > imgHeight )
{
// Create a resizing ratio.
double ratio = (double) imgWidth / (double)
widthConstraint;
int newHeight = (int) ( (double) imgHeight / ratio );
// use Image.getScaledInstance( w, h,
// constant), where constant is a constant
// pulled from the Image class indicating how
// process the image; smooth image, fast
// processing, etc.
adjustedImg = new ImageIcon(
fromStream.getImage().getS
widthConstraint, newHeight,
Image.SCALE_SMOOTH )
);
}
else
{
// Create a resizing ratio.
double ratio = (double) imgHeight / (double)
heightConstraint;
int newWidth = (int) ( (double) imgWidth / ratio );
adjustedImg = new ImageIcon(
fromStream.getImage().getS
heightConstraint, Image.SCALE_SMOOTH )
);
}
// return the adjusted ImageIcon object.
return adjustedImg;
}
else
{
// Assure the resources from the adjustedImg object
// are released and then return the original ImageIcon
// object if the submitted image's width and height
// already fell within the given constraints.
adjustedImg = null;
return fromStream;
}
}
**********
The problem is that the when I add the Jlabel in the panel the it uses the same space as it wasn't resized, is like the function doesn't work but checking the values of the imageIcon with getIconHeight() the values are correct, so why when I add the Jlabel in the panel the sized is not changed??
thank you!!
(not necessarily your problem though)
Do you revalidate() the layout after adding the new label?
ASKER
The label is added at the beginnig and is not changed so I think I don't need to call revalidate()...
Aren't you changing the icon (and thus its size)?
ASKER
No, I take the picture and resize it at the begining and I create the IconImage, so I don't change it:
ImageIcon pic = new ImageIcon( url );
pic = resizeImage( new ImageIcon( url ), 300, 300 );
JLabel pictureLabel = new JLabel( pic );
mainPanel.add( pictureLabel );
mainPanel.add( dataPanel );
mainPanel.add( trackPanel );
mainPanel.add( editorScrollPane );
setLocation( 150, 20 );
setSize( 700, 700 );
setVisible( true );
ImageIcon pic = new ImageIcon( url );
pic = resizeImage( new ImageIcon( url ), 300, 300 );
JLabel pictureLabel = new JLabel( pic );
mainPanel.add( pictureLabel );
mainPanel.add( dataPanel );
mainPanel.add( trackPanel );
mainPanel.add( editorScrollPane );
setLocation( 150, 20 );
setSize( 700, 700 );
setVisible( true );
And you're saying that the icon returned from resizeImage has the correct size, but the label is displaying it at the original size, is that correct?
ASKER
correct!
> and use it like an ImageIcon inside a JLabel in a GridLayout
GridLayout lays out all its components the same size, ignoring their preferred size.
GridLayout lays out all its components the same size, ignoring their preferred size.
ASKER
The problem is that the biggest element is the ImageIcon, so all the components have the size of the ImageIcon, but not from the ImageIcon after resizing it with resizeImage instead the size is the one of the ImageIcon before resizing it... that is the problem...
The size of the imageicon has nothing to do with how it is layed out with a GridLayout it is sized to fit into its grid.
The size of the parent container is what will affect the size of each grid cell.
The size of the parent container is what will affect the size of each grid cell.
ASKER
For my understanding the size of each grid cell, will be the size of the maximun cell, so in my application the cells are too big because it gets the size from the ImageIcon even if this icon has been resized, I just want that this ImageIcon will have a size of 300, 300 so all the cells will be 300, 300... is that correct?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That should be ||, not |
if ( imgWidth > widthConstraint || imgHeight > heightConstraint )