Java JButton Settings scaleButtonIcon(Icon icon, int size)

Here you can find the source of scaleButtonIcon(Icon icon, int size)

Description

scale Button Icon

License

Open Source License

Declaration

private static Icon scaleButtonIcon(Icon icon, int size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Image;

import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main {
    private static final double ICON_SCALE_THRESHOLD = 0.2;

    private static Icon scaleButtonIcon(Icon icon, int size) {
        Icon result = icon;/*from w w  w.  j  a v  a2  s  .  com*/
        if (icon != null) {
            int h = icon.getIconHeight();
            if ((size > (1.0 + ICON_SCALE_THRESHOLD) * h) || (size < (1.0 - ICON_SCALE_THRESHOLD) * h)) {
                int w = icon.getIconWidth();
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                icon.paintIcon(new JButton(), image.getGraphics(), 0, 0);
                double ratio = (h == 0) ? 0.0 : (double) w / (double) h;
                int width = (int) Math.round(ratio * size);
                Image scaleImage = image.getScaledInstance(width, size, Image.SCALE_SMOOTH);
                result = new ImageIcon(scaleImage);
            }
        }
        return result;
    }
}

Related

  1. removeAllActionListeners(AbstractButton btn)
  2. removeButtonBorder(AbstractButton button)
  3. removeCloseButton(Component comp)
  4. removeListeners(AbstractButton button)
  5. scaleAllAbstractButtonIconsOf(Container container, int size)
  6. ScaleButtonIcon(JToggleButton btn, int width, int height, int fontsize)
  7. SelectedOptionButton(Container container)
  8. selectFile(final int openMode, final String title, final String buttonText, final String lastDirectoryUsed, final Component parent, final String suffix, final String description)
  9. selectWithoutNotifyingListeners(AbstractButton ab, boolean selected)