Java Icon to BufferedImage toBufferedImage(Icon icon, int x, int y)

Here you can find the source of toBufferedImage(Icon icon, int x, int y)

Description

Converts the given Icon into a BufferedImage .

License

Open Source License

Parameter

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

Return

The created .

Declaration

public static BufferedImage toBufferedImage(Icon icon, 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.jav a 2  s.  c  o 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. getBufferedImage(Component comp, Icon icon)
  2. getBufferedImage(Icon icon)
  3. getBufferedImageFromIcon(Icon icon)
  4. iconToBufferedImage(Icon icon)
  5. toBufferedImage(Icon icon)