get Pressed effect Icon - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

get Pressed effect Icon

Demo Code


//package com.java2s;
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  w  ww .j ava  2  s  .  com

        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 Tutorials