Java Swing Icon grayIcon(Icon icon)

Here you can find the source of grayIcon(Icon icon)

Description

Make grascale using a ColorConvertOp

License

Open Source License

Declaration

public static ImageIcon grayIcon(Icon icon) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from ww w.j av  a  2 s. c o  m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Graphics;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {
    /**
     * Make grascale using a {@link ColorConvertOp}
     */
    public static ImageIcon grayIcon(Icon icon) {
        if (icon == null || !(icon instanceof ImageIcon)) {
            return null;
        }
        BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.createGraphics();
        g.drawImage(((ImageIcon) icon).getImage(), 0, 0, null);
        g.dispose();
        ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
        return new ImageIcon(op.filter(image, image));
    }
}

Related

  1. getScaledIcon(Icon tmpIcon, int w, int h, boolean incr)
  2. getScaledIcon(String resourceName, int width, int height)
  3. getSize(String graphIconPath)
  4. getTransparentIcon(int width)
  5. getVerBumpIcon()
  6. iconFromStream(final InputStream in)
  7. item(String name, Icon icon)
  8. loadIcon(Class pRootClass, String strPath)
  9. loadIcon(ClassLoader classLoader, String path)