001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins.streetside.utils; 003 004import java.awt.Image; 005 006import javax.swing.ImageIcon; 007 008import org.openstreetmap.josm.tools.Logging; 009 010public final class ImageUtil { 011 private ImageUtil() { 012 // Private constructor to avoid instantiation 013 } 014 015 /** 016 * Scales an {@link ImageIcon} to the desired size 017 * @param icon the icon, which should be resized 018 * @param size the desired length of the longest edge of the icon 019 * @return the resized {@link ImageIcon}. It is the same object that you put in, 020 * only the contained {@link Image} is exchanged. 021 */ 022 public static ImageIcon scaleImageIcon(final ImageIcon icon, int size) { 023 Logging.debug("Scale icon {0} → {1}", icon.getIconWidth(), size); 024 return new ImageIcon(icon.getImage().getScaledInstance( 025 icon.getIconWidth() >= icon.getIconHeight() ? size : Math.max(1, Math.round(icon.getIconWidth() / (float) icon.getIconHeight() * size)), 026 icon.getIconHeight() >= icon.getIconWidth() ? size : Math.max(1, Math.round(icon.getIconHeight() / (float) icon.getIconWidth() * size)), 027 Image.SCALE_SMOOTH 028 )); 029 } 030}