Java BufferedImage Create getBufferedImage(String imageFile, Component c)

Here you can find the source of getBufferedImage(String imageFile, Component c)

Description

Creates a BufferedImage from the given image file.

License

Open Source License

Parameter

Parameter Description
imageFile an image file
c an AWT component

Declaration

public static BufferedImage getBufferedImage(String imageFile, Component c) 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import java.awt.Component;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;

import java.awt.image.BufferedImage;

import java.util.HashMap;

public class Main {
    private static HashMap<String, BufferedImage> imageCache = new HashMap<String, BufferedImage>();

    /**/*  www .  j  ava 2 s .c o  m*/
     * Creates a <code>BufferedImage</code> from the given image file.
     * 
     * @param imageFile
     *            an image file
     * @param c
     *            an AWT component
     */
    public static BufferedImage getBufferedImage(String imageFile, Component c) {
        if (imageFile == null || c == null) {
            return null;
        }

        BufferedImage bufferedImage = (BufferedImage) imageCache.get(imageFile);
        if (bufferedImage != null) {
            return bufferedImage;
        }

        Image image = c.getToolkit().getImage(imageFile);
        MediaTracker tracker = new MediaTracker(c);
        tracker.addImage(image, 0);
        try {
            tracker.waitForAll();
        } catch (InterruptedException e) {
        }

        bufferedImage = new BufferedImage(image.getWidth(c), image.getHeight(c), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.drawImage(image, 0, 0, c);
        imageCache.put(imageFile, bufferedImage);

        return bufferedImage;
    }
}

Related

  1. convertToBufferedImage(BufferedImage image, int imageType)
  2. getBufferedImage(byte[] imgStr)
  3. getBufferedImage(Component comp, BufferedImage image)
  4. getBufferedImage(File file)
  5. getBufferedImage(String imagePath)
  6. getBufferedImageAsType(int type, BufferedImage image, int sizeX, int sizeY)
  7. getBufferedImageBytes(BufferedImage bufferedImage)
  8. getBufferedImaged(Image img, int width, int height)