Java Utililty Methods Swing Icon

List of utility methods to do Swing Icon

Description

The list of methods to do Swing Icon are organized into topic(s).

Method

booleanareSizesEqual(javax.swing.Icon prevIcon, javax.swing.Icon nextIcon)
are Sizes Equal
int prevWidth = prevIcon != null ? prevIcon.getIconWidth() : 0;
int prevHeight = prevIcon != null ? prevIcon.getIconHeight() : 0;
int nextWidth = nextIcon != null ? nextIcon.getIconWidth() : 0;
int nextHeight = nextIcon != null ? nextIcon.getIconHeight() : 0;
return (prevWidth == nextWidth) && (prevHeight == nextHeight);
ImageIconblendIcon(Icon icon, float iconWeight, int rgb, float rgbWeight)
Add a color layer to the icon (not including alpha)
if (icon == null || !(icon instanceof ImageIcon)) {
    return null;
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
        BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(((ImageIcon) icon).getImage(), 0, 0, null);
g.dispose();
...
voidblitBitmap(Graphics g, Icon icon, int x, int y, int w, int h)
Paint a backing bitmap into the specified rectangle of the passed Graphics object.
Shape clip = g.getClip();
if (clip == null) {
    g.setClip(x, y, w, h);
} else {
    scratch.setBounds(x, y, w, h);
    Area area = new Area(clip);
    area.intersect(new Area(scratch));
    g.setClip(area);
...
IconbuildIcon(final String relativePath, final Class loader)
build Icon
URL resource = loader.getResource(relativePath);
if (resource != null) {
    return new ImageIcon(resource);
} else {
    System.err.println("[ImageHelper] Invalid resource: " + relativePath + " Loader: " + loader.getName());
    throw new IllegalArgumentException("Invalid resource");
IconchangeBrightness(Icon icon, float factor)
Brightens icons of type ImageIcon by a factor
if (icon == null || !(icon instanceof ImageIcon)) {
    return icon;
ImageIcon imageIcon = (ImageIcon) icon;
ImageIcon ret = new ImageIcon();
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
        BufferedImage.TYPE_INT_ARGB);
image.getGraphics().drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
...
voidclearMergedIconsCache()
clear Merged Icons Cache
mergedIconsCache.clear();
IconcombineIcons(Icon underIcon, Icon overIcon)
Returns an icon that combines the 2 specified icons.
int newWidth = Math.max(underIcon.getIconWidth(), overIcon.getIconWidth());
int newHeight = Math.max(underIcon.getIconHeight(), overIcon.getIconHeight());
BufferedImage combinedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = combinedImage.createGraphics();
underIcon.paintIcon(null, g2d, 0, 0);
overIcon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
return new ImageIcon(combinedImage);
...
IconconcatenateIcons(final Icon icon1, final Icon icon2)
Concatenates 2 icons next to each other (in 1 row).
return new Icon() {
    @Override
    public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
        icon1.paintIcon(c, g, x, y);
        icon2.paintIcon(c, g, x + icon1.getIconWidth(), y);
    @Override
    public int getIconWidth() {
...
BufferedImagecreateBitmap(Icon icon)
create Bitmap
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
        BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
icon.paintIcon(null, g, 0, 0);
g.dispose();
return bi;
ImageIconcreateChannelIcon(Icon ic)
Creates a scaled Version of the Icon.
BufferedImage img = new BufferedImage(42, 22, BufferedImage.TYPE_INT_RGB);
if (ic == null) {
    ic = new ImageIcon("./imgs/tvbrowser16.png");
int height = 20;
int width = 40;
if ((ic.getIconWidth() != 0) && (ic.getIconHeight() != 0)) {
    double iWidth = ic.getIconWidth();
...