Java Image to BufferedImage toBufferedImage(Image image, int x, int y)

Here you can find the source of toBufferedImage(Image image, int x, int y)

Description

Converts the given Image into a BufferedImage .

License

Open Source License

Parameter

Parameter Description
image The Image to convert.
x The x position to place image at.
y The y position to place image at.

Return

The created with the content from the given .

Declaration

public static BufferedImage toBufferedImage(Image image, int x, int y) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w w  w  .j  a  v  a  2  s . co m*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.Transparency;
import java.awt.image.BufferedImage;

import javax.swing.Icon;

public class Main {
    /**
     * Converts the given {@link Image} into a {@link BufferedImage}.
     * @param image The {@link Image} to convert.
     * @return The created {@link BufferedImage} with the content from the given {@link Image}.
     */
    public static BufferedImage toBufferedImage(Image image) {
        return toBufferedImage(image, 0, 0);
    }

    /**
     * Converts the given {@link Image} into a {@link BufferedImage}.
     * @param image The {@link Image} to convert.
     * @param x The x position to place image at.
     * @param y The y position to place image at.
     * @return The created {@link BufferedImage} with the content from the given {@link Image}.
     */
    public static BufferedImage toBufferedImage(Image image, int x, int y) {
        BufferedImage result = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = result.getGraphics();
        try {
            g.drawImage(image, x, y, null);
            return result;
        } finally {
            g.dispose();
        }
    }

    /**
     * <p>
     * Converts the given {@link Icon} into a {@link BufferedImage}.
     * </p>
     * <p>
     * Fore more details have a look at
     * <a href="http://www.java2s.com/Tutorial/Java/0280__SWT/ConvertsanAWTimagetoSWT.htm">http://www.java2s.com/Tutorial/Java/0280__SWT/ConvertsanAWTimagetoSWT.htm</a>.
     * </p>
     * @param icon The {@link Icon} to convert.
     * @return The created {@link BufferedImage}.
     */
    public static BufferedImage toBufferedImage(Icon icon) {
        return toBufferedImage(icon, 0, 0);
    }

    /**
     * <p>
     * Converts the given {@link Icon} into a {@link BufferedImage}.
     * </p>
     * <p>
     * Fore more details have a look at
     * <a href="http://www.java2s.com/Tutorial/Java/0280__SWT/ConvertsanAWTimagetoSWT.htm">http://www.java2s.com/Tutorial/Java/0280__SWT/ConvertsanAWTimagetoSWT.htm</a>.
     * </p>
     * @param icon The {@link Icon} to convert.
     * @param x The x position to place image at.
     * @param y The y position to place image at.
     * @return The created {@link BufferedImage}.
     */
    public static BufferedImage toBufferedImage(Icon icon, int x, int y) {
        if (icon != null) {
            Graphics2D g = null;
            try {
                BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                        Transparency.TRANSLUCENT);
                g = image.createGraphics();
                icon.paintIcon(null, g, x, y);
                return image;
            } finally {
                if (g != null) {
                    g.dispose();
                }
            }
        } else {
            return null;
        }
    }
}

Related

  1. toBufferedImage(Image image)
  2. toBufferedImage(Image image)
  3. toBufferedImage(Image image)
  4. toBufferedImage(Image image)
  5. toBufferedImage(Image image, int type)
  6. toBufferedImage(Image img)
  7. toBufferedImage(Image img)
  8. toBufferedImage2(Image image)
  9. toGrayBufferedImage(Image image)