< prev index next >

src/java.desktop/share/classes/sun/swing/CachedPainter.java

Print this page

        

@@ -103,46 +103,18 @@
                          int y, int w, int h, Object... args) {
         Object key = getClass();
         GraphicsConfiguration config = getGraphicsConfiguration(c);
         ImageCache cache = getCache(key);
         Image image = cache.getImage(key, config, w, h, args);
-        int attempts = 0;
-        do {
-            boolean draw = false;
-            if (image instanceof VolatileImage) {
-                // See if we need to recreate the image
-                switch (((VolatileImage)image).validate(config)) {
-                case VolatileImage.IMAGE_INCOMPATIBLE:
-                    ((VolatileImage)image).flush();
-                    image = null;
-                    break;
-                case VolatileImage.IMAGE_RESTORED:
-                    draw = true;
-                    break;
-                }
-            }
+
             if (image == null) {
-                // Recreate the image
-                image = createImage(c, w, h, config, args);
+            image = new PainterMultiResolutionCachedImage(c, w, h, args);
                 cache.setImage(key, config, w, h, args, image);
-                draw = true;
-            }
-            if (draw) {
-                // Render to the Image
-                Graphics g2 = image.getGraphics();
-                paintToImage(c, image, g2, w, h, args);
-                g2.dispose();
             }
 
             // Render to the passed in Graphics
             paintImage(c, g, x, y, w, h, image, args);
-
-            // If we did this 3 times and the contents are still lost
-            // assume we're painting to a VolatileImage that is bogus and
-            // give up.  Presumably we'll be called again to paint.
-        } while ((image instanceof VolatileImage) &&
-                 ((VolatileImage)image).contentsLost() && ++attempts < 3);
     }
 
     /**
      * Paints the representation to cache to the supplied Graphics.
      *

@@ -208,6 +180,82 @@
         if (c == null) {
             return null;
         }
         return c.getGraphicsConfiguration();
     }
+
+    class PainterMultiResolutionCachedImage extends AbstractMultiResolutionImage {
+
+        private final int baseWidth;
+        private final int baseHeight;
+        private final Component c;
+        private final Object[] args;
+
+        public PainterMultiResolutionCachedImage(Component c, int w, int h,
+                                                 Object[] args) {
+            this.c = c;
+            this.args = args;
+            this.baseWidth = w;
+            this.baseHeight = h;
+        }
+
+        @Override
+        public Image getResolutionVariant(double destWidth, double destHeight) {
+
+            int w = (int) Math.ceil(destWidth);
+            int h = (int) Math.ceil(destHeight);
+
+            Object key = this;
+            GraphicsConfiguration config = getGraphicsConfiguration(c);
+            ImageCache cache = getCache(key);
+            Image image = cache.getImage(key, config, w, h, args);
+            int attempts = 0;
+            do {
+                boolean draw = false;
+                if (image instanceof VolatileImage) {
+                    // See if we need to recreate the image
+                    switch (((VolatileImage) image).validate(config)) {
+                        case VolatileImage.IMAGE_INCOMPATIBLE:
+                            ((VolatileImage) image).flush();
+                            image = null;
+                            break;
+                        case VolatileImage.IMAGE_RESTORED:
+                            draw = true;
+                            break;
+                    }
+                }
+                if (image == null) {
+                    // Recreate the image
+                    image = createImage(c, w, h, config, args);
+                    cache.setImage(key, config, w, h, args, image);
+                    draw = true;
+                }
+                if (draw) {
+                    // Render to the Image
+                    Graphics g2 = image.getGraphics();
+                    paintToImage(c, image, g2, w, h, args);
+                    g2.dispose();
+                }
+
+                // If we did this 3 times and the contents are still lost
+                // assume we're painting to a VolatileImage that is bogus and
+                // give up.  Presumably we'll be called again to paint.
+            } while ((image instanceof VolatileImage)
+                    && ((VolatileImage) image).contentsLost() && ++attempts < 3);
+
+            return image;
+        }
+
+        @Override
+        protected Image getBaseImage() {
+            return getResolutionVariant(baseWidth, baseHeight);
+        }
+
+        @Override
+        public java.util.List<Image> getResolutionVariants() {
+            return Arrays.asList(
+                    getResolutionVariant(baseWidth, baseHeight),
+                    getResolutionVariant(2 * baseWidth, 2 * baseHeight)
+            );
+        }
+    }
 }
< prev index next >