Java Utililty Methods ImageIcon Resize

List of utility methods to do ImageIcon Resize

Description

The list of methods to do ImageIcon Resize are organized into topic(s).

Method

ImageIcongetResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth, int iRequiredHeight)
The method will scale down the sourceImageIcon in a way that it will fit into the rectangle with dimensions (iRequiredWidth, iRequiredHeight) with the original aspect ratio being preserved.
int iWidth = sourceImageIcon.getIconWidth();
int iHeight = sourceImageIcon.getIconHeight();
float fWidthResizeRatio = iWidth / (float) iRequiredWidth;
float fHeightResizeRatio = iHeight / (float) iRequiredHeight;
float fResizeRatio = Math.max(fWidthResizeRatio, fHeightResizeRatio);
int iNewWidth = Math.round(iWidth / fResizeRatio);
int iNewHeight = Math.round(iHeight / fResizeRatio);
BufferedImage resizedImage = new BufferedImage(iNewWidth, iNewHeight, BufferedImage.TYPE_INT_RGB);
...
ImageIconresize(ImageIcon icon, int extent)
resize
return new ImageIcon(icon.getImage().getScaledInstance(extent, extent, Image.SCALE_SMOOTH));
ImageIconresize(ImageIcon image, int width, int height)
resize
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(
        new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image.getImage(), 0, 0, width, height, null);
g2d.dispose();
return new ImageIcon(bi);
ImageIconresize(ImageIcon src, Dimension size)
resize
if (src != null)
    return new ImageIcon(src.getImage().getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH));
return null;
ImageIconresize(ImageIcon src, int destWidth, int destHeight)
Resizies an image using SCALE_SMOOTH (slow)
return new ImageIcon(src.getImage().getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH));
ImageresizeImageAspectRatio(int width, int height, ImageIcon orig_img)
Resize image aspect ratio.
Image img = null;
float disp_aspect_ratio = (height * 1.0F) / width;
float aspect_ratio = (orig_img.getIconHeight() * 1.0F) / orig_img.getIconWidth();
if (aspect_ratio == disp_aspect_ratio) {
    img = orig_img.getImage().getScaledInstance(width, height, 0);
} else if (aspect_ratio < disp_aspect_ratio) {
    img = orig_img.getImage().getScaledInstance(width, (int) (height * aspect_ratio / disp_aspect_ratio),
            0);
...
ImageIconresizeImageIcon(ImageIcon icon, int width, int height)
Resize the input ImageIcon to a new size
BufferedImage buffered = convert(icon);
BufferedImage resized = resizeImage(buffered, width, height);
ImageIcon result = new ImageIcon(resized);
return result;
ImageIconto50x50(ImageIcon imageIcon)
tox
return resizeTo(imageIcon, new Dimension(50, 50));