Java Swing Icon getIcon(boolean isPressed, Icon baseIcon_)

Here you can find the source of getIcon(boolean isPressed, Icon baseIcon_)

Description

get Icon

License

Open Source License

Declaration

public static Icon getIcon(boolean isPressed, Icon baseIcon_) 

Method Source Code


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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;

public class Main {
    private static final JComponent TEMP = new JLabel();

    public static Icon getIcon(boolean isPressed, Icon baseIcon_) {
        BufferedImage img = new BufferedImage(baseIcon_.getIconWidth(), baseIcon_.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();

        baseIcon_.paintIcon(TEMP, g, 0, 0);
        g.dispose();/*from   ww w. ja  va 2 s. c  o m*/

        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                Color rgba = new Color(img.getRGB(i, j), true);
                int alpha = rgba.getAlpha();

                if (isPressed) {
                    rgba = rgba.darker();
                } else {
                    rgba = rgba.brighter();
                }

                rgba = new Color(rgba.getRed(), rgba.getGreen(), rgba.getBlue(), alpha);

                img.setRGB(i, j, rgba.getRGB());
            }
        }

        return new ImageIcon(img);
    }
}

Related

  1. getDefaultIcon()
  2. getDefaultIcon()
  3. getEmptyIcon()
  4. getFolderIcon()
  5. getFolderIcon()
  6. getIcon(Class baseclass, String name)
  7. getIcon(Class clazz, String path)
  8. getIcon(final String path)
  9. getIcon(final String resource)