Here you can find the source of paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)
public static Image paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)
//package com.java2s; /*/*from w ww . j av a 2 s . c o m*/ * Copyright 2010, 2011 Institut Pasteur. * * This file is part of ICY. * * ICY 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. * * ICY 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 ICY. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; public class Main { /** * Paint the specified color in 'out' image depending original alpha intensity from 'alphaImage' */ public static Image paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color) { final int w; final int h; final Image result; if (out == null) { w = alphaImage.getWidth(null); h = alphaImage.getHeight(null); if ((w == -1) || (h == -1)) return null; result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); } else { w = out.getWidth(null); h = out.getHeight(null); if ((w == -1) || (h == -1)) return null; result = out; } final Graphics2D g = (Graphics2D) result.getGraphics(); // clear g.setBackground(new Color(0x00000000, true)); g.clearRect(0, 0, w, h); // draw icon g.drawImage(alphaImage, 0, 0, null); // set fill color g.setComposite(AlphaComposite.SrcAtop); g.setColor(color); g.fillRect(0, 0, w, h); g.dispose(); return result; } }