Here you can find the source of scaleButtonIcon(Icon icon, int size)
private static Icon scaleButtonIcon(Icon icon, int size)
//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; } }