Hi,

I have an application here, that has a lot of buttons with image icons,
all have the same icon, and many of them get disabled. This leads to
gray-filtering the icons, and since they are all the same, the
grayfilter filters the same icon over and over again. I think this can
be optimized by introducing a cache like in the attached patch.

Now I am not sure if this change is feasible. There is a (very small)
chance that somebody uses this to filter changing images. For example,
somebody could filter a BufferedImage, then draw into that and filter it
again (to refresh the gray representation or so). I would guess that
nobody really does this, but what do I know? In this case, he would get
the old gray image in return.

What do you think about that, could this be included in OpenJDK?

Cheers, Roman

-- 
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com   * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
diff -r a36c38fe9ff1 src/share/classes/javax/swing/GrayFilter.java
--- a/src/share/classes/javax/swing/GrayFilter.java	Tue Apr 22 16:57:20 2008 +0200
+++ b/src/share/classes/javax/swing/GrayFilter.java	Wed Apr 23 14:40:16 2008 +0200
@@ -26,6 +26,9 @@ package javax.swing;
 
 import java.awt.*;
 import java.awt.image.*;
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
 
 /**
  * An image filter that "disables" an image by turning
@@ -38,6 +41,14 @@ import java.awt.image.*;
  * @author      Jim Graham
  */
 public class GrayFilter extends RGBImageFilter {
+
+    /**
+     * Caches already gray-filtered images for faster access in
+     * [EMAIL PROTECTED] #createDisabledImage(Image)}.
+     */
+    private static Map<Image,Image> grayImageCache =
+        Collections.synchronizedMap(new WeakHashMap<Image,Image>());
+
     private boolean brighter;
     private int percent;
 
@@ -45,9 +56,17 @@ public class GrayFilter extends RGBImage
      * Creates a disabled image
      */
     public static Image createDisabledImage (Image i) {
-        GrayFilter filter = new GrayFilter(true, 50);
-        ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
-        Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
+        Image grayImage;
+        Image cached = grayImageCache.get(i);
+        if (cached == null) {
+            GrayFilter filter = new GrayFilter(true, 50);
+            ImageProducer prod = new FilteredImageSource(i.getSource(),
+                    filter);
+            grayImage = Toolkit.getDefaultToolkit().createImage(prod);
+            grayImageCache.put(i, grayImage);
+        } else {
+            grayImage = cached;
+        }
         return grayImage;
     }
 

Reply via email to