Image-resizing methods I took a little heat on a method I had to resize an image and I'm hoping I can get a little help on the best way to do it. This is intended for a simple Java Helper Library I'm developing. I have a few ways to do it.

New Method:

/**
* This method resizes the given image
*
* @param image
* @param width
* @param height
* @param max if true, sets the width and height as maximum heights and widths, if false, they are minimums
* @return
*/
public static BufferedImage resizeImage(BufferedImage image, int width, int height, boolean max) {
  int type = (image.getTransparency() == Transparency.OPAQUE)
          ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
  BufferedImage bi = new BufferedImage(width, height, type);
  Graphics2D graphics = bi.createGraphics();
  graphics.drawImage(image, 0, 0, width, height, null);
  graphics.dispose();
  return bi;
}


Original Method:

/**
* This method resizes the given image
*
* @param width
* @param height
* @param max if true, sets the width and height as maximum heights and widths, if false, they are minimums
* @return
*/
public static ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max) {
  Image image = imageIcon.getImage();
  Image newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
  int width1 = newimg.getWidth(null);
  if ((max &amp;&amp; width1 &gt; width) || (!max &amp;&amp; width1 &lt; width)) {
    newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
  }
  return new ImageIcon(newimg);
}


Are these kinds of convenience methods ok?

/**
* Convenience method. Creates ImageIcon from the given file location and calls resizeImage with it
*
* @param location
* @param width
* @param height
* @param max
* @return
*/
public static ImageIcon resizeImageFromFile(String location, int width, int height, boolean max) {
  ImageIcon imageIcon = new ImageIcon(location);
  return resizeImage(imageIcon, width, height, max);
}

/**
* Convenience method. Creates ImageIcon from the given resource location and calls resizeImage with it
*
* @param location
* @param width
* @param height
* @param max
* @return
*/
public static ImageIcon resizeImageFromResource(String location, int width, int height, boolean max) {
  ImageIcon imageIcon = new ImageIcon(SwingHelper.class.getResource(location));
  return resizeImage(imageIcon, width, height, max);
}
Edit: about the new method, I'd say Graphics2D is a bit overkill (look at Konrad Rudolph's comment for another opinion). And you don't use the max parameter anymore. Does it stretch the image?

The original method is better, but you should have a real if else: resizing the image twice is a hack. It would look like this: you just have to fill in the if.

public static ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max) {
  Image image = imageIcon.getImage();
  Image newimg;
  if(/* think what condition would work here */) {
    newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
  } else {
    newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
  }
  return new ImageIcon(newimg);
}


I think the helper methods are useless since there is no abstraction: it just does two separate things. This does not help that much, and it's better if opening the file is explicit: the code is easier to read.