Java Graphics Draw paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)

Here you can find the source of paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)

Description

Paint the specified color in 'out' image depending original alpha intensity from 'alphaImage'

License

Open Source License

Declaration

public static Image paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color) 

Method Source Code


//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;
    }
}

Related

  1. paint(Graphics2D g)
  2. paint3Deffect(Graphics2D g2D, JComponent c, boolean round, boolean out)
  3. paint3DRectEffect(Graphics graphics, int x, int y, int width, int height)
  4. paint3DRoundRectEffect(Graphics g, int x, int y, int width, int height, int radius)
  5. paintBall(Graphics2D g2, Color c)
  6. paintComponent(Graphics g, Component c, Container p, Rectangle r)
  7. paintCross(Graphics g, Color color, int centerX, int centerY, int size, int width)
  8. paintFilthyPanel(Graphics g, int width, int height)
  9. paintFocus(Graphics g, int x, int y, int width, int height, int r1, int r2, float grosor, Color color)