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.apache.log4j.Logger; 009import org.openstreetmap.josm.tools.I18n; 010 011public final class ImageUtil { 012 013 final static Logger logger = Logger.getLogger(ImageUtil.class); 014 015 private ImageUtil() { 016 // Private constructor to avoid instantiation 017 } 018 019 /** 020 * Scales an {@link ImageIcon} to the desired size 021 * @param icon the icon, which should be resized 022 * @param size the desired length of the longest edge of the icon 023 * @return the resized {@link ImageIcon}. It is the same object that you put in, 024 * only the contained {@link Image} is exchanged. 025 */ 026 public static ImageIcon scaleImageIcon(final ImageIcon icon, int size) { 027 if(StreetsideProperties.DEBUGING_ENABLED.get()) { 028 logger.debug(I18n.tr("Scale icon {0} → {1}", icon.getIconWidth(), size)); 029 } 030 return new ImageIcon(icon.getImage().getScaledInstance( 031 icon.getIconWidth() >= icon.getIconHeight() ? size : Math.max(1, Math.round(icon.getIconWidth() / (float) icon.getIconHeight() * size)), 032 icon.getIconHeight() >= icon.getIconWidth() ? size : Math.max(1, Math.round(icon.getIconHeight() / (float) icon.getIconWidth() * size)), 033 Image.SCALE_SMOOTH 034 )); 035 } 036}