All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.swing.IconResizer Maven / Gradle / Ivy

There is a newer version: 2.13.61
Show newest version
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.kenai.com
 * SCM: https://kenai.com/hg/openbluesky~src
 *
 **********************************************************************************************************************/
package it.tidalwave.swing;

import java.awt.Image;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import org.openide.util.ImageUtilities;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class IconResizer
  {
    public static JComponent fix (JComponent component, int size)
      {
        return fix(new JComponent[]{component}, size)[0];
      }
    
    public static JComponent[] fix (JComponent[] components, int size)
      {
        for (int i = 0; i < components.length; i++)
          {
            if (components[i] instanceof AbstractButton)
              {
                AbstractButton button = (AbstractButton)components[i];
                
                if (button.getIcon() != null)
                  {
                    button.setIcon(scaledIcon(button.getIcon(), size));
                  }
                
                if (button.getDisabledIcon() != null)
                  {
                    button.setDisabledIcon(scaledIcon(button.getDisabledIcon(), size));
                  }
              }
          }
        
        return components;
      }
    
    public static Image scaledImage (Image image, int maxIconSize)
      {
        if (image == null)
          {
            return image;  
          }
        
        ImageIcon icon = (ImageIcon)scaledIcon(new ImageIcon(image), maxIconSize);
        return icon.getImage();
      }
    
    public static Icon scaledIcon (Icon icon, int maxIconSize)
      {
        if (icon != null)
          {
            int width = icon.getIconWidth();
            int height = icon.getIconHeight();
            final int maxSize = Math.max(width, height);
            
            if (maxSize > maxIconSize)
              {
                final double scale = (double)maxIconSize / maxSize;
                width = (int) Math.round(scale * width);
                height = (int) Math.round(scale * height);
                final Image image = ImageUtilities.icon2Image(icon).getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
                return ImageUtilities.image2Icon(image);
              }
          }
        
        return icon;
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy