Java Image Create getImage(final int width, final int height)

Here you can find the source of getImage(final int width, final int height)

Description

Creates an image of specified dimension, filled with the background color.

License

Open Source License

Parameter

Parameter Description
width Positive integer representing the width of the image.
height Positive integer representing the height of the image.

Return

BufferedImage of specified dimension.

Declaration

public static BufferedImage getImage(final int width, final int height) 

Method Source Code


//package com.java2s;
/*/*from w  w w.ja va 2s .com*/
 *  Copyright (C) 2010 vektor
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import javax.swing.UIManager;

public class Main {
    /**
     * Creates an image of specified dimension, filled with the background color.
     *
     * @param width Positive integer representing the width of the image.
     * @param height Positive integer representing the height of the image.
     * @return BufferedImage of specified dimension.
     */
    public static BufferedImage getImage(final int width, final int height) {
        return getImage(width, height, getColorBackground());
    }

    /**
     * Creates an image of specified dimension, filled with the specified color.
     *
     * @param width Positive integer representing the width of the image.
     * @param height Positive integer representing the height of the image.
     * @param color Background color of the image.
     * @return BufferedImage of specified dimension.
     */
    public static BufferedImage getImage(final int width, final int height, final Color color) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("Image dimension must be positive integers.");
        }
        final BufferedImage ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics2D g = ret.createGraphics();
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        return ret;
    }

    /**
     * Returns the background color for the rule displayer.
     *
     * @return Background color for the rule displayer - taken from the background
     * color of a tabbed pane.
     */
    public static Color getColorBackground() {
        return UIManager.getDefaults().getColor("TabbedPane.background");
    }
}

Related

  1. createImagePanel(Image image, Color backColor)
  2. getImage(Class clazz, String path)
  3. getImage(Component component)
  4. getImage(Component component)
  5. getImage(File imageFile)
  6. getImage(final String name)
  7. getImage(ImageIcon imageIcon)
  8. getImage(JComponent component)
  9. getImage(Object obj, String filePath)