KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > laures > cewolf > util > ImageHelper


1 /* ================================================================
2  * Cewolf : Chart enabling Web Objects Framework
3  * ================================================================
4  *
5  * Project Info: http://cewolf.sourceforge.net
6  * Project Lead: Guido Laures (guido@laures.de);
7  *
8  * (C) Copyright 2002, by Guido Laures
9  *
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.
13  *
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.
17  *
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.
21  */

22
23 package de.laures.cewolf.util;
24
25 import java.awt.Component JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Image JavaDoc;
28 import java.awt.MediaTracker JavaDoc;
29 import java.awt.image.BufferedImage JavaDoc;
30 import java.awt.image.ColorModel JavaDoc;
31 import java.awt.image.PixelGrabber JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Some simple image rendering helper methods.
38  * @author Guido Laures
39  */

40 public class ImageHelper {
41
42     private static final Component JavaDoc comp = new Component JavaDoc() { };
43     private static final MediaTracker JavaDoc tracker = new MediaTracker JavaDoc(comp);
44     private static final Log log = LogFactory.getLog(ImageHelper.class);
45
46     /** Creates a new instance of ImageHelper */
47     private ImageHelper() {
48     }
49
50     public static final Image JavaDoc loadImage(String JavaDoc fileName) {
51         final Image JavaDoc image = java.awt.Toolkit.getDefaultToolkit().getImage(fileName);
52         synchronized(tracker) {
53             tracker.addImage(image, 0);
54             try {
55                 tracker.waitForID(0, 0);
56             } catch (InterruptedException JavaDoc e) {
57                 log.debug("INTERRUPTED while loading Image");
58             }
59             tracker.removeImage(image, 0);
60         }
61         return image;
62     }
63
64     public static BufferedImage JavaDoc loadBufferedImage(String JavaDoc fileName) {
65         Image JavaDoc image = loadImage(fileName);
66         if (image instanceof BufferedImage JavaDoc) {
67             return (BufferedImage JavaDoc)image;
68         }
69         /* final boolean hasAlpha = hasAlpha(image);
70        int transparency = Transparency.OPAQUE;
71         if (hasAlpha) {
72             transparency = Transparency.BITMASK;
73         }*/

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 JavaDoc bimage = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_RGB);
78         final Graphics JavaDoc g = bimage.createGraphics();
79         g.drawImage(image, 0, 0, null);
80         g.dispose();
81         return bimage;
82     }
83
84     public static boolean hasAlpha(Image JavaDoc image) {
85         if (image instanceof BufferedImage JavaDoc) {
86             return ((BufferedImage JavaDoc)image).getColorModel().hasAlpha();
87         }
88         PixelGrabber JavaDoc pg = new PixelGrabber JavaDoc(image, 0, 0, 1, 1, false);
89         try {
90             pg.grabPixels();
91         } catch (InterruptedException JavaDoc e) {
92             e.printStackTrace();
93         }
94         ColorModel JavaDoc cm = pg.getColorModel();
95         if(cm == null){
96             return false;
97         }
98         return cm.hasAlpha();
99     }
100
101     public static BufferedImage JavaDoc createImage(int width, int height) {
102         // return GRAPHICS_CONV.createCompatibleImage(width, height);
103
return new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_RGB);
104     }
105
106 }
107
Popular Tags