Java Image to BufferedImage makeBufferedImage(Image image)

Here you can find the source of makeBufferedImage(Image image)

Description

Creates a buffered image from the supplied image.

License

Open Source License

Parameter

Parameter Description
image The supplied image.

Return

The buffered image.

Declaration

public static BufferedImage makeBufferedImage(Image image) 

Method Source Code

//package com.java2s;
/*//from   w w w  . ja  v a2 s  .  c  o  m
 * Copyright (C) 2010-2011, Gostai S.A.S.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

import java.awt.*;

import java.awt.image.BufferedImage;

public class Main {
    /** The component. */
    private static final Component component = new Component() {
    };
    /** The mediatracker.associated with the component */
    private static final MediaTracker mediatracker = new MediaTracker(
            component);
    /** The id of the image. */
    private static int id = 0;
    /** The width of the image. */
    private static int width = 0;
    /** The height of the image. */
    private static int height = 0;

    /**
     * Creates a buffered image from the supplied image.
     * <p>
     * @param image  The supplied image.
     * @return  The buffered image.
     */
    public static BufferedImage makeBufferedImage(Image image) {
        if (image == null)
            System.out.println("ImageUtilities l.67 Image null");
        return makeBufferedImage(image, BufferedImage.TYPE_INT_RGB);
    }

    /**
     * Creates a buffered image from the supplied image and its type.
     * <p>
     * @param image  The supplied image.
     * @param imageType The type of the image.
     * @return  The buffered image.
     */
    public static BufferedImage makeBufferedImage(Image image, int imageType) {
        if (waitForImage(image) == false)
            return null;
        if (image == null)
            System.out.println("ImageUtilities l.77 Image null");
        BufferedImage bufferedImage = new BufferedImage(width, height,
                imageType);

        Graphics2D g = bufferedImage.createGraphics();
        if (image == null)
            System.out.println("ImageUtilities l.80 Image null");
        g.drawImage(image, null, null);
        return bufferedImage;
    }

    /**
     * Waits for the given image to load fully. Returns true if
     * everything goes well or false if there is an error while loading
     * the image.
     * <p>
     * @return true if everything goes well or false if there is an error
     * during loading the image.
     */
    public static boolean waitForImage(Image image) {
        int i;

        synchronized (component) {
            i = id++;
        }
        mediatracker.addImage(image, i, width, height);
        try {
            mediatracker.waitForID(i);
        } catch (InterruptedException ie) {
            return false;
        }
        if (mediatracker.isErrorID(i))
            return false;
        return true;
    }
}

Related

  1. imageToBufferedImage(Image img)
  2. imageToBufferedImage(Image pImage)
  3. imageToBufferedImage(Image src)
  4. makeBufferedImage(final Image image)
  5. makeBufferedImage(Image image)
  6. makeRGBABufferedImageFromImage( Image image)
  7. toBufferedImage(Image i)
  8. toBufferedImage(Image image)
  9. toBufferedImage(Image image)