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