[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Resize an ImageIcon

Posted on 2005-04-11
15
Medium Priority
?
5,862 Views
Last Modified: 2012-06-27
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!!
0
Comment
Question by:ximotur
  • 8
  • 5
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13757710
>   if ( imgWidth > widthConstraint | imgHeight >  heightConstraint )

That should be ||, not |

  if ( imgWidth > widthConstraint || imgHeight >  heightConstraint )

0
 
LVL 92

Expert Comment

by:objects
ID: 13757714
(not necessarily your problem though)
0
 
LVL 92

Expert Comment

by:objects
ID: 13757716
Do you revalidate() the layout after adding the new label?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ximotur
ID: 13757871

The label is added at the beginnig and is not changed so I think I don't need to call revalidate()...
0
 
LVL 92

Expert Comment

by:objects
ID: 13757905
Aren't you changing the icon (and thus its size)?
0
 

Author Comment

by:ximotur
ID: 13757994
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
 
LVL 92

Expert Comment

by:objects
ID: 13758020
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
 

Author Comment

by:ximotur
ID: 13758211
correct!
0
 
LVL 92

Expert Comment

by:objects
ID: 13758234
> 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
 

Author Comment

by:ximotur
ID: 13764364
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
 
LVL 92

Expert Comment

by:objects
ID: 13767745
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
 

Author Comment

by:ximotur
ID: 13768469

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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13768503
No thats not correct, the size of each cell is determined by the size of the parent and and number of rows/columns.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question