01: // The contents of this file are subject to the Mozilla Public License Version
02: // 1.1
03: //(the "License"); you may not use this file except in compliance with the
04: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
05: //
06: //Software distributed under the License is distributed on an "AS IS" basis,
07: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
08: //for the specific language governing rights and
09: //limitations under the License.
10: //
11: //The Original Code is "The Columba Project"
12: //
13: //The Initial Developers of the Original Code are Frederik Dietz and Timo
14: // Stich.
15: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16: //
17: //All Rights Reserved.
18: package org.columba.core.gui.base;
19:
20: import java.awt.Image;
21: import java.awt.MediaTracker;
22: import java.awt.Toolkit;
23: import java.awt.image.FilteredImageSource;
24: import java.awt.image.ImageFilter;
25: import java.awt.image.ImageProducer;
26:
27: import javax.swing.ImageIcon;
28: import javax.swing.JPanel;
29:
30: public class ImageUtil {
31: /** Create a 20% Transparent icon */
32: public static ImageIcon createTransparentIcon(ImageIcon icon) {
33: return createTransparentIcon(icon, 20);
34: }
35:
36: /** Create a x% Transparent icon */
37: public static ImageIcon createTransparentIcon(ImageIcon icon,
38: int percentage) {
39: return createIcon(icon, new TransparentFilter(percentage));
40: }
41:
42: /** Create a new icon which is filtered by some ImageFilter */
43: private static synchronized ImageIcon createIcon(ImageIcon icon,
44: ImageFilter filter) {
45: ImageProducer ip;
46: Image image;
47: MediaTracker tracker;
48:
49: ip = new FilteredImageSource(icon.getImage().getSource(),
50: filter);
51: image = Toolkit.getDefaultToolkit().createImage(ip);
52:
53: tracker = new MediaTracker(new JPanel());
54: tracker.addImage(image, 1);
55:
56: try {
57: tracker.waitForID(1);
58: } catch (InterruptedException e) {
59: e.printStackTrace();
60:
61: return null;
62: }
63:
64: return new ImageIcon(image);
65: }
66: }
|