Java examples for 2D Graphics:BufferedImage
Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real screen devices (e.g.
/*//from w w w . java2s . c om * {{{ header & license * Copyright (c) 2007 Patrick Wright * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * }}} */ //package com.java2s; import java.awt.*; import java.awt.image.BufferedImage; public class Main { /** * Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real * screen devices (e.g. not in headless mode), the image will be compatible with the screen device allowing * for best performance. In a headless environment, simply creates a new BufferedImage. For non-headless * environments, this just sets up and calls * {@link java.awt.GraphicsConfiguration#createCompatibleImage(int,int,int)}. The image will not have anything * drawn to it, not even a white background; you must do this yourself. The {@link #clearBackground(BufferedImage)} * method will do this for you if you like. * * @param width Target width for the image * @param height Target height for the image * @param biType Value from the {@link java.awt.image.BufferedImage} class; see docs for * {@link java.awt.image.BufferedImage#BufferedImage(int,int,int)}. The actual type used will * be the type specified in this parameter, if in headless mode, or the type most compatible with the screen, if * in non-headless more. * @return A BufferedImage compatible with the screen (best fit). */ public static BufferedImage createCompatibleBufferedImage(int width, int height, int biType) { BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); if (ge.isHeadlessInstance()) { bimage = new BufferedImage(width, height, biType); } else { GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); // TODO: check type using image type - can be sniffed; see Filthy Rich Clients int type = (biType == BufferedImage.TYPE_INT_ARGB || biType == BufferedImage.TYPE_INT_ARGB_PRE ? Transparency.TRANSLUCENT : Transparency.OPAQUE); bimage = gc.createCompatibleImage(width, height, type); } return bimage; } /** * Creates a BufferedImage compatible with the local graphics environment; this is a helper method for a * common process and just sets up and calls * {@link java.awt.GraphicsConfiguration#createCompatibleImage(int,int,int)}. The image will support * transparent pixels. * * @param width Target width for the image * @param height Target height for the image * @return A BufferedImage compatible with the screen (best fit) supporting transparent pixels. */ public static BufferedImage createCompatibleBufferedImage(int width, int height) { return createCompatibleBufferedImage(width, height, Transparency.BITMASK); } }