< prev index next >

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

Print this page




  88      * @param w Width to render in
  89      * @param h Height to render in
  90      * @param args Variable arguments that will be passed to paintToImage
  91      */
  92     public void paint(Component c, Graphics g, int x,
  93                          int y, int w, int h, Object... args) {
  94         if (w <= 0 || h <= 0) {
  95             return;
  96         }
  97         synchronized (CachedPainter.class) {
  98             paint0(c, g, x, y, w, h, args);
  99         }
 100     }
 101 
 102     private void paint0(Component c, Graphics g, int x,
 103                          int y, int w, int h, Object... args) {
 104         Object key = getClass();
 105         GraphicsConfiguration config = getGraphicsConfiguration(c);
 106         ImageCache cache = getCache(key);
 107         Image image = cache.getImage(key, config, w, h, args);
 108         int attempts = 0;
 109         do {
 110             boolean draw = false;
 111             if (image instanceof VolatileImage) {
 112                 // See if we need to recreate the image
 113                 switch (((VolatileImage)image).validate(config)) {
 114                 case VolatileImage.IMAGE_INCOMPATIBLE:
 115                     ((VolatileImage)image).flush();
 116                     image = null;
 117                     break;
 118                 case VolatileImage.IMAGE_RESTORED:
 119                     draw = true;
 120                     break;
 121                 }
 122             }
 123             if (image == null) {
 124                 // Recreate the image
 125                 image = createImage(c, w, h, config, args);
 126                 cache.setImage(key, config, w, h, args, image);
 127                 draw = true;
 128             }
 129             if (draw) {
 130                 // Render to the Image
 131                 Graphics g2 = image.getGraphics();
 132                 paintToImage(c, image, g2, w, h, args);
 133                 g2.dispose();
 134             }
 135 
 136             // Render to the passed in Graphics
 137             paintImage(c, g, x, y, w, h, image, args);
 138 
 139             // If we did this 3 times and the contents are still lost
 140             // assume we're painting to a VolatileImage that is bogus and
 141             // give up.  Presumably we'll be called again to paint.
 142         } while ((image instanceof VolatileImage) &&
 143                  ((VolatileImage)image).contentsLost() && ++attempts < 3);
 144     }
 145 
 146     /**
 147      * Paints the representation to cache to the supplied Graphics.
 148      *
 149      * @param c Component painting to, may be null.
 150      * @param image Image to paint to
 151      * @param g Graphics to paint to, obtained from the passed in Image.
 152      * @param w Width to paint to
 153      * @param h Height to paint to
 154      * @param args Arguments supplied to <code>paint</code>
 155      */
 156     protected abstract void paintToImage(Component c, Image image, Graphics g,
 157                                          int w, int h, Object[] args);
 158 
 159 
 160     /**
 161      * Paints the image to the specified location.
 162      *
 163      * @param c Component painting to


 193             return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
 194         }
 195         return config.createCompatibleVolatileImage(w, h);
 196     }
 197 
 198     /**
 199      * Clear the image cache
 200      */
 201     protected void flush() {
 202         synchronized(CachedPainter.class) {
 203             getCache(getClass()).flush();
 204         }
 205     }
 206 
 207     private GraphicsConfiguration getGraphicsConfiguration(Component c) {
 208         if (c == null) {
 209             return null;
 210         }
 211         return c.getGraphicsConfiguration();
 212     }












































































 213 }


  88      * @param w Width to render in
  89      * @param h Height to render in
  90      * @param args Variable arguments that will be passed to paintToImage
  91      */
  92     public void paint(Component c, Graphics g, int x,
  93                          int y, int w, int h, Object... args) {
  94         if (w <= 0 || h <= 0) {
  95             return;
  96         }
  97         synchronized (CachedPainter.class) {
  98             paint0(c, g, x, y, w, h, args);
  99         }
 100     }
 101 
 102     private void paint0(Component c, Graphics g, int x,
 103                         int y, int w, int h, Object... args) {
 104         Object key = getClass();
 105         GraphicsConfiguration config = getGraphicsConfiguration(c);
 106         ImageCache cache = getCache(key);
 107         Image image = cache.getImage(key, config, w, h, args);
 108 














 109         if (image == null) {
 110             image = new PainterMultiResolutionCachedImage(c, w, h, args);

 111             cache.setImage(key, config, w, h, args, image);







 112         }
 113 
 114         // Render to the passed in Graphics
 115         paintImage(c, g, x, y, w, h, image, args);






 116     }
 117 
 118     /**
 119      * Paints the representation to cache to the supplied Graphics.
 120      *
 121      * @param c Component painting to, may be null.
 122      * @param image Image to paint to
 123      * @param g Graphics to paint to, obtained from the passed in Image.
 124      * @param w Width to paint to
 125      * @param h Height to paint to
 126      * @param args Arguments supplied to <code>paint</code>
 127      */
 128     protected abstract void paintToImage(Component c, Image image, Graphics g,
 129                                          int w, int h, Object[] args);
 130 
 131 
 132     /**
 133      * Paints the image to the specified location.
 134      *
 135      * @param c Component painting to


 165             return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
 166         }
 167         return config.createCompatibleVolatileImage(w, h);
 168     }
 169 
 170     /**
 171      * Clear the image cache
 172      */
 173     protected void flush() {
 174         synchronized(CachedPainter.class) {
 175             getCache(getClass()).flush();
 176         }
 177     }
 178 
 179     private GraphicsConfiguration getGraphicsConfiguration(Component c) {
 180         if (c == null) {
 181             return null;
 182         }
 183         return c.getGraphicsConfiguration();
 184     }
 185 
 186     class PainterMultiResolutionCachedImage extends AbstractMultiResolutionImage {
 187 
 188         private final int baseWidth;
 189         private final int baseHeight;
 190         private final Component c;
 191         private final Object[] args;
 192 
 193         public PainterMultiResolutionCachedImage(Component c, int w, int h,
 194                                                  Object[] args) {
 195             this.c = c;
 196             this.args = args;
 197             this.baseWidth = w;
 198             this.baseHeight = h;
 199         }
 200 
 201         @Override
 202         public Image getResolutionVariant(double destWidth, double destHeight) {
 203 
 204             int w = (int) Math.ceil(destWidth);
 205             int h = (int) Math.ceil(destHeight);
 206 
 207             Object key = this;
 208             GraphicsConfiguration config = getGraphicsConfiguration(c);
 209             ImageCache cache = getCache(key);
 210             Image image = cache.getImage(key, config, w, h, args);
 211             int attempts = 0;
 212             do {
 213                 boolean draw = false;
 214                 if (image instanceof VolatileImage) {
 215                     // See if we need to recreate the image
 216                     switch (((VolatileImage) image).validate(config)) {
 217                         case VolatileImage.IMAGE_INCOMPATIBLE:
 218                             ((VolatileImage) image).flush();
 219                             image = null;
 220                             break;
 221                         case VolatileImage.IMAGE_RESTORED:
 222                             draw = true;
 223                             break;
 224                     }
 225                 }
 226                 if (image == null) {
 227                     // Recreate the image
 228                     image = createImage(c, w, h, config, args);
 229                     cache.setImage(key, config, w, h, args, image);
 230                     draw = true;
 231                 }
 232                 if (draw) {
 233                     // Render to the Image
 234                     Graphics g2 = image.getGraphics();
 235                     paintToImage(c, image, g2, w, h, args);
 236                     g2.dispose();
 237                 }
 238 
 239                 // If we did this 3 times and the contents are still lost
 240                 // assume we're painting to a VolatileImage that is bogus and
 241                 // give up.  Presumably we'll be called again to paint.
 242             } while ((image instanceof VolatileImage)
 243                     && ((VolatileImage) image).contentsLost() && ++attempts < 3);
 244 
 245             return image;
 246         }
 247 
 248         @Override
 249         protected Image getBaseImage() {
 250             return getResolutionVariant(baseWidth, baseHeight);
 251         }
 252 
 253         @Override
 254         public java.util.List<Image> getResolutionVariants() {
 255             return Arrays.asList(
 256                     getResolutionVariant(baseWidth, baseHeight),
 257                     getResolutionVariant(2 * baseWidth, 2 * baseHeight)
 258             );
 259         }
 260     }
 261 }
< prev index next >