composite Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

composite Image

Demo Code


//package com.java2s;

import java.awt.Component;
import java.awt.Image;

import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

public class Main {
    public static Image composite(Component who, Image im, Image mask) {
        int w = im.getWidth(who);
        int h = im.getHeight(who);
        int ipix[] = new int[w * h];
        int mpix[] = new int[w * h];
        boolean gotfg = false;
        boolean gotma = false;
        try {/*from w  ww.  java2  s .c o  m*/
            // note that there are problems with pixelgrabber from images
            // created with
            // createimage, but apparently not with images from loadimage
            gotfg = new PixelGrabber(im, 0, 0, w, h, ipix, 0, w)
                    .grabPixels();
            gotma = new PixelGrabber(mask, 0, 0, w, h, mpix, 0, w)
                    .grabPixels();
        } catch (InterruptedException e) {
        }
        if (gotfg && gotma) {
            for (int i = 0; i < w * h; i++) {
                int ma = 0xff - (mpix[i] & 0xff); // center pixel
                int fg = ipix[i];
                ipix[i] = (fg & 0xffffff) | (ma << 24);
            }
            Image fin = who.createImage(new MemoryImageSource(w, h, ipix,
                    0, w));
            return (fin);
        }
        System.out.println("Composite failed");

        return (im);
    }
}

Related Tutorials