Sets the background of the image to the specified color - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

Sets the background of the image to the specified color

Demo Code

/*//from w  ww .j  a  va2  s.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 {
    /**
     * Sets the background of the image to the specified color
     *
     * @param image   the image
     * @param bgColor the color
     */
    public static void clearImage(BufferedImage image, Color bgColor) {
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setColor(bgColor);
        g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
        g2d.dispose();
    }

    /**
     * Sets the background of the image to white.
     *
     * @param image the image
     */
    public static void clearImage(BufferedImage image) {
        clearImage(image, Color.WHITE);
    }
}

Related Tutorials