1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
8 * (C) Copyright 2002, by Guido Laures
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
23 package de.laures.cewolf.util;
25 import java.awt.Component;
26 import java.awt.Graphics;
27 import java.awt.Image;
28 import java.awt.MediaTracker;
29 import java.awt.image.BufferedImage;
30 import java.awt.image.ColorModel;
31 import java.awt.image.PixelGrabber;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
37 * Some simple image rendering helper methods.
38 * @author Guido Laures
40 public class ImageHelper {
42 private static final Component comp = new Component() { };
43 private static final MediaTracker tracker = new MediaTracker(comp);
44 private static final Log log = LogFactory.getLog(ImageHelper.class);
46 /** Creates a new instance of ImageHelper */
47 private ImageHelper() {
50 public static final Image loadImage(String fileName) {
51 final Image image = java.awt.Toolkit.getDefaultToolkit().getImage(fileName);
52 synchronized(tracker) {
53 tracker.addImage(image, 0);
55 tracker.waitForID(0, 0);
56 } catch (InterruptedException e) {
57 log.debug("INTERRUPTED while loading Image");
59 tracker.removeImage(image, 0);
64 public static BufferedImage loadBufferedImage(String fileName) {
65 Image image = loadImage(fileName);
66 if (image instanceof BufferedImage) {
67 return (BufferedImage)image;
69 /* final boolean hasAlpha = hasAlpha(image);
70 int transparency = Transparency.OPAQUE;
72 transparency = Transparency.BITMASK;
74 int width = (int)Math.max(1.0, image.getWidth(null));
75 int height = (int)Math.max(1.0, image.getHeight(null));
76 // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
77 BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
78 final Graphics g = bimage.createGraphics();
79 g.drawImage(image, 0, 0, null);
84 public static boolean hasAlpha(Image image) {
85 if (image instanceof BufferedImage) {
86 return ((BufferedImage)image).getColorModel().hasAlpha();
88 PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
91 } catch (InterruptedException e) {
94 ColorModel cm = pg.getColorModel();
101 public static BufferedImage createImage(int width, int height) {
102 // return GRAPHICS_CONV.createCompatibleImage(width, height);
103 return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);