Java Utililty Methods ImageIcon Scale

List of utility methods to do ImageIcon Scale

Description

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

Method

ImageIcongetScaledImageIcon(final String location, final int width, final int height)
Get an image as a resource and create an ImageIcon
ImageIcon icon = null;
URL url = Thread.currentThread().getContextClassLoader().getResource(location);
if (url != null) {
    icon = new ImageIcon(url);
    icon = new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
return (icon);
ImageIcongetScaledImageIconHeight(ImageIcon ii, int h, boolean incr)
get Scaled Image Icon Height
if (ii != null) {
    if (ii.getIconWidth() > h || incr) {
        ii = new ImageIcon(ii.getImage().getScaledInstance(-1, h, Image.SCALE_DEFAULT));
return ii;
ImageIconrescale(ImageIcon src, Dimension newMinSize, ImageObserver observer)
Rescales an icon.
double widthRatio = newMinSize.getWidth() / (double) src.getIconWidth();
double heightRatio = newMinSize.getHeight() / (double) src.getIconHeight();
double scale = widthRatio > heightRatio ? widthRatio : heightRatio;
int w = (int) Math.round(scale * src.getIconWidth());
int h = (int) Math.round(scale * src.getIconHeight());
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = dst.createGraphics();
g2.drawImage(src.getImage(), 0, 0, w, h, observer);
...
ImageIconrescaleImageIcon(ImageIcon image, int size)
rescale Image Icon
final BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
final Image cursorImage = image.getImage();
g2d.drawImage(cursorImage, new AffineTransform((double) size / cursorImage.getWidth(null), 0, 0,
        (double) size / cursorImage.getHeight(null), 0, 0), null);
return new ImageIcon(img);
ImageIconscale(ImageIcon icon, int maxWidth, int maxHeight)
scale
if (icon == null)
    return null;
if (maxWidth >= icon.getIconWidth() && maxHeight >= icon.getIconHeight())
    return icon;
int width = Math.min(maxWidth, icon.getIconWidth()), height = Math.min(maxHeight, icon.getIconHeight());
float widthFactor = (float) width / icon.getIconWidth(),
        heightFactor = (float) height / icon.getIconHeight();
if (widthFactor < heightFactor) {
...
ImageIconscale(ImageIcon icon, int newHeight, int newWidth)
Returns a scaled down image if the height or width is smaller than the image size.
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
boolean scaleHeight = height * newWidth > width * newHeight;
if (height > newHeight) {
    if (width <= newWidth || scaleHeight) {
        height = newHeight;
        width = -1;
...
ImageIconscale(ImageIcon original, int width, int height)
scale
if (original == null) {
    throw new IllegalArgumentException("original image is NULL");
Image img = original.getImage();
Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
IconscaleIcon(final ImageIcon icon)
Returns a scaled icon based on the reduced font size
int scaledWidth = (int) Math.floor(icon.getIconWidth() * growthPercentage);
int scaledHeight = (int) Math.floor(icon.getIconHeight() * growthPercentage);
Image scaledInstance = icon.getImage().getScaledInstance(scaledWidth, scaledHeight,
        java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(scaledInstance);
ImageIconscaleIcon(ImageIcon icon, int size)
scale Icon
return new ImageIcon(scaleIcon(icon.getImage(), size));
ImageIconscaleIcon(int width, int height, ImageIcon img)
Scales the given image icon to the input width/height
return new ImageIcon(scaleImage(width, height, img.getImage()));