Here you can find the source of getBufferedImage(String imageFile, Component c)
BufferedImage
from the given image file.
Parameter | Description |
---|---|
imageFile | an image file |
c | an AWT component |
public static BufferedImage getBufferedImage(String imageFile, Component c)
//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; } }