Here you can find the source of swapColor(Image image, Color imageColor, Color newColor)
Parameter | Description |
---|---|
image | a parameter |
imageColor | a parameter |
newColor | a parameter |
public static Image swapColor(Image image, Color imageColor, Color newColor)
//package com.java2s; /*/*from ww w.j ava 2 s. co m*/ * Copyright appNativa Inc. All Rights Reserved. * * This file is part of the Real-time Application Rendering Engine (RARE). * * RARE is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ import java.awt.Color; import java.awt.Component; import java.awt.Composite; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.FilteredImageSource; import java.awt.image.RGBImageFilter; import javax.swing.Icon; public class Main { /** * {@inheritDoc} * * @param image {@inheritDoc} * @param imageColor {@inheritDoc} * @param newColor {@inheritDoc} * * @return {@inheritDoc} */ public static Image swapColor(Image image, Color imageColor, Color newColor) { final int irgb = imageColor.getRGB(); final int nrgb = newColor.getRGB(); RGBImageFilter filter = new RGBImageFilter() { @Override public int filterRGB(int x, int y, int rgb) { if (rgb == irgb) { return nrgb; } return rgb; } }; FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(filteredSrc); } public static BufferedImage createImage(Component comp) { Dimension size = comp.getSize(); if ((size.width < 1) || (size.height < 1)) { size.width = 1; size.height = 1; } BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TRANSLUCENT); Graphics2D g2 = image.createGraphics(); try { comp.paint(g2); } finally { g2.dispose(); } return image; } public static BufferedImage createImage(Component comp, Icon icon) { return createImage(comp, icon, null); } public static BufferedImage createImage(Component comp, Icon icon, Composite composite) { if (icon == null) { return null; } BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TRANSLUCENT); Graphics2D g2 = image.createGraphics(); if (composite != null) { g2.setComposite(composite); } try { icon.paintIcon(comp, g2, 0, 0); } finally { g2.dispose(); } return image; } }