Java JButton Settings setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation, final double scale)

Here you can find the source of setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation, final double scale)

Description

Sets the button's default icon, scales it to a given factor of the button's preferred size and aligns it to the given orientation.

License

Creative Commons License

Parameter

Parameter Description
button The button.
icon The icon.
orientation Either <code>SwingConstants.HORIZONTAL</code> or <code>SwingConstants.VERTICAL</code>.
scale The scale of the icon.

Declaration

public static void setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation,
        final double scale) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.AbstractButton;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

public class Main {
    /**//from   w w  w  .j a v  a 2 s  . c  o  m
    Sets the button's default icon,
     scales it to a given factor of the button's preferred size and
     aligns it to the given orientation.
        
    Use with care:
        
    <pre>
    Helpers.setScaledIcon(new JButton("Example"), new ImageIcon("example.png"), SwingConstants.VERTICAL, 0.5);
    </pre>
        
    @param button The button.
    @param icon The icon.
    @param orientation Either <code>SwingConstants.HORIZONTAL</code> or <code>SwingConstants.VERTICAL</code>.
    @param scale The scale of the icon.
    **/
    public static void setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation,
            final double scale) {
        final Dimension preferredSize = button.getPreferredSize();
        final Insets margin = button.getMargin();
        final int minimumSize = Math.min(preferredSize.width - margin.left - margin.right,
                preferredSize.height - margin.top - margin.bottom);
        int size = minimumSize;
        final double actualScale = Math.abs(scale);
        if (actualScale != 1) {
            final double scaledSize = actualScale * size;
            if (scaledSize <= Integer.MAX_VALUE)
                size = (int) scaledSize;
        }
        final Image image = icon.getImage().getScaledInstance(size, size, Image.SCALE_SMOOTH);
        button.setIcon(new ImageIcon(image));
        switch (orientation) {
        case SwingConstants.HORIZONTAL:
            button.setVerticalTextPosition(SwingConstants.CENTER);
            button.setHorizontalTextPosition(SwingConstants.TRAILING);
            break;
        case SwingConstants.VERTICAL:
            button.setVerticalTextPosition(SwingConstants.BOTTOM);
            button.setHorizontalTextPosition(SwingConstants.CENTER);
            break;
        default:
            throw new IllegalArgumentException();
        }
    }

    /**
    Sets the button's default icon,
     scales it to the button's preferred size and
     aligns it to the given orientation.
        
    Use with care:
        
    <pre>
    Helpers.setScaledIcon(new JButton("Example"), new ImageIcon("example.png"), SwingConstants.VERTICAL);
    </pre>
        
    @param button The button.
    @param icon The icon.
    @param orientation Either <code>SwingConstants.HORIZONTAL</code> or <code>SwingConstants.VERTICAL</code>.
    **/
    public static void setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation) {
        setScaledIcon(button, icon, orientation, 1);
    }
}

Related

  1. setHorizontalMargin(AbstractButton button, int hMargin)
  2. setImageIcon(AbstractButton abstractButton, URL url)
  3. setMnemonic(AbstractButton actionComponent)
  4. setPreferredWidth(JComponent button, int minWidth)
  5. setRadioButtonSelected(Window window, String buttonText)
  6. setSelected(final AbstractButton abstractButton, final boolean isSelected)
  7. setSelectedButton(ButtonGroup group, int index)
  8. setSelectedSilently(AbstractButton x, boolean b)
  9. setToggleGroups(final JToggleButton... buttons)