Creates a compatible image given the parameters. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

Creates a compatible image given the parameters.

Demo Code


//package com.java2s;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;

public class Main {
    /** The Constant GFX_CONFIG. */
    private static final GraphicsConfiguration GFX_CONFIG = GraphicsEnvironment
            .getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration();/*from  w  w  w.  j a  v  a 2  s. c o m*/

    /**
     * Creates a compatible image given the parameters.
     *
     * @param width
     *            the width
     * @param height
     *            the height
     * @param transparency
     *            the transparency
     * @return the buffered image
     */
    public static BufferedImage createImage(final int width,
            final int height, final int transparency) {
        BufferedImage image = GFX_CONFIG.createCompatibleImage(width,
                height, transparency);
        if (image.getRaster().getDataBuffer().getDataType() != DataBuffer.TYPE_INT) {
            switch (transparency) {
            case Transparency.TRANSLUCENT:
                image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_ARGB);
                break;
            case Transparency.OPAQUE:
                image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
                break;
            case Transparency.BITMASK:
                image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_ARGB_PRE);
                break;
            default:
                break;
            }
        }
        return image;
    }
}

Related Tutorials