set Pixels BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Pixel

Description

set Pixels BufferedImage

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

public class Main {
    public static void setPixels(BufferedImage img, int x, int y, int w,
            int h, int[] pixels) {
        if (pixels == null || w == 0 || h == 0) {
            return;
        } else if (pixels.length < w * h) {
            throw new IllegalArgumentException(
                    "pixels array must have a length" + " >= w*h"); //$NON-NLS-1$ //$NON-NLS-2$
        }/*from   ww w.  ja v  a 2 s .  c o  m*/

        int imageType = img.getType();
        if (imageType == BufferedImage.TYPE_INT_ARGB
                || imageType == BufferedImage.TYPE_INT_RGB) {
            WritableRaster raster = img.getRaster();
            raster.setDataElements(x, y, w, h, pixels);
        } else {
            // Unmanages the image
            img.setRGB(x, y, w, h, pixels, 0, w);
        }
    }
}

Related Tutorials