KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > ImageUtil


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//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 JavaDoc;
21 import java.awt.MediaTracker JavaDoc;
22 import java.awt.Toolkit JavaDoc;
23 import java.awt.image.FilteredImageSource JavaDoc;
24 import java.awt.image.ImageFilter JavaDoc;
25 import java.awt.image.ImageProducer JavaDoc;
26
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29
30
31 public class ImageUtil {
32     /** Create a 20% Transparent icon */
33     public static ImageIcon JavaDoc createTransparentIcon(ImageIcon JavaDoc icon) {
34         return createTransparentIcon(icon, 20);
35     }
36
37     /** Create a x% Transparent icon */
38     public static ImageIcon JavaDoc createTransparentIcon(ImageIcon JavaDoc icon, 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 JavaDoc createIcon(ImageIcon JavaDoc icon,
44         ImageFilter JavaDoc filter) {
45         ImageProducer JavaDoc ip;
46         Image JavaDoc image;
47         MediaTracker JavaDoc tracker;
48
49         ip = new FilteredImageSource JavaDoc(icon.getImage().getSource(), filter);
50         image = Toolkit.getDefaultToolkit().createImage(ip);
51
52         tracker = new MediaTracker JavaDoc(new JPanel JavaDoc());
53         tracker.addImage(image, 1);
54
55         try {
56             tracker.waitForID(1);
57         } catch (InterruptedException JavaDoc e) {
58             e.printStackTrace();
59
60             return null;
61         }
62
63         return new ImageIcon JavaDoc(image);
64     }
65 }
66
Popular Tags