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().getScaledInstance(
        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().getScaledInstance( 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!!
ximoturAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
No thats not correct, the size of each cell is determined by the size of the parent and and number of rows/columns.
0
 
objectsCommented:
>   if ( imgWidth > widthConstraint | imgHeight >  heightConstraint )

That should be ||, not |

  if ( imgWidth > widthConstraint || imgHeight >  heightConstraint )

0
 
objectsCommented:
(not necessarily your problem though)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
objectsCommented:
Do you revalidate() the layout after adding the new label?
0
 
ximoturAuthor Commented:

The label is added at the beginnig and is not changed so I think I don't need to call revalidate()...
0
 
objectsCommented:
Aren't you changing the icon (and thus its size)?
0
 
ximoturAuthor Commented:
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 );
0
 
objectsCommented:
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?
0
 
ximoturAuthor Commented:
correct!
0
 
objectsCommented:
> 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.
0
 
ximoturAuthor Commented:
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...

0
 
objectsCommented:
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.
0
 
ximoturAuthor Commented:

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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.