Returns a *copy* of a subimage of image. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Returns a *copy* of a subimage of image.

Demo Code


//package com.java2s;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

public class Main {
    /**//from  w  w w. j  av a  2 s.c  o m
     * Returns a *copy* of a subimage of image. This avoids the performance problems associated with BufferedImage.getSubimage.
     * @param image the image
     * @param x the x position
     * @param y the y position
     * @param w the width
     * @param h the height
     * @return the subimage
     */
    public static BufferedImage getSubimage(BufferedImage image, int x,
            int y, int w, int h) {
        BufferedImage newImage = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = newImage.createGraphics();
        g.drawRenderedImage(image,
                AffineTransform.getTranslateInstance(-x, -y));
        g.dispose();
        return newImage;
    }
}

Related Tutorials