7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.swing; 26 27 import java.awt.*; 28 import java.awt.image.*; 29 import java.util.*; 30 31 /** 32 * A base class used for icons or images that are expensive to paint. 33 * A subclass will do the following: 34 * <ol> 35 * <li>Invoke <code>paint</code> when you want to paint the image, 36 * if you are implementing <code>Icon</code> you'll invoke this from 37 * <code>paintIcon</code>. 38 * The args argument is useful when additional state is needed. 39 * <li>Override <code>paintToImage</code> to render the image. The code that 40 * lives here is equivalent to what previously would go in 41 * <code>paintIcon</code>, for an <code>Icon</code>. 42 * </ol> 43 * The two ways to use this class are: 44 * <ol> 45 * <li>Invoke <code>paint</code> to draw the cached reprensentation at 46 * the specified location. 47 * <li>Invoke <code>getImage</code> to get the cached reprensentation and 48 * draw the image yourself. This is primarly useful when you are not 49 * using <code>VolatileImage</code>. 112 int attempts = 0; 113 VolatileImage volatileImage = (image instanceof VolatileImage) 114 ? (VolatileImage) image 115 : null; 116 do { 117 boolean draw = false; 118 if (volatileImage != null) { 119 // See if we need to recreate the image 120 switch (volatileImage.validate(config)) { 121 case VolatileImage.IMAGE_INCOMPATIBLE: 122 volatileImage.flush(); 123 image = null; 124 break; 125 case VolatileImage.IMAGE_RESTORED: 126 draw = true; 127 break; 128 } 129 } 130 if (image == null) { 131 // Recreate the image 132 image = createImage(c, w, h, config, args); 133 cache.setImage(key, config, w, h, args, image); 134 draw = true; 135 volatileImage = (image instanceof VolatileImage) 136 ? (VolatileImage) image 137 : null; 138 } 139 if (draw) { 140 // Render to the Image 141 Graphics2D g2 = (Graphics2D) image.getGraphics(); 142 if (volatileImage == null && (w != baseWidth || h != baseHeight)) { 143 g2.scale((double) w / baseWidth, (double) h / baseHeight); 144 } 145 paintToImage(c, image, g2, baseWidth, baseHeight, args); 146 g2.dispose(); 147 } 148 149 // If we did this 3 times and the contents are still lost 150 // assume we're painting to a VolatileImage that is bogus and 151 // give up. Presumably we'll be called again to paint. 152 } while ((volatileImage != null) && 153 volatileImage.contentsLost() && ++attempts < 3); 154 155 return image; 156 } 157 158 private void paint0(Component c, Graphics g, int x, 159 int y, int w, int h, Object... args) { 160 Object key = getClass(); 161 GraphicsConfiguration config = getGraphicsConfiguration(c); 162 ImageCache cache = getCache(key); 163 Image image = cache.getImage(key, config, w, h, args); 164 165 if (image == null) { | 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.swing; 26 27 import sun.awt.image.SurfaceManager; 28 import sun.java2d.SurfaceData; 29 import java.awt.Component; 30 import java.awt.Graphics; 31 import java.awt.Graphics2D; 32 import java.awt.GraphicsConfiguration; 33 import java.awt.Image; 34 import java.awt.geom.AffineTransform; 35 import java.awt.image.AbstractMultiResolutionImage; 36 import java.awt.image.BufferedImage; 37 import java.awt.image.ImageObserver; 38 import java.awt.image.VolatileImage; 39 import java.util.Arrays; 40 import java.util.HashMap; 41 import java.util.Map; 42 43 /** 44 * A base class used for icons or images that are expensive to paint. 45 * A subclass will do the following: 46 * <ol> 47 * <li>Invoke <code>paint</code> when you want to paint the image, 48 * if you are implementing <code>Icon</code> you'll invoke this from 49 * <code>paintIcon</code>. 50 * The args argument is useful when additional state is needed. 51 * <li>Override <code>paintToImage</code> to render the image. The code that 52 * lives here is equivalent to what previously would go in 53 * <code>paintIcon</code>, for an <code>Icon</code>. 54 * </ol> 55 * The two ways to use this class are: 56 * <ol> 57 * <li>Invoke <code>paint</code> to draw the cached reprensentation at 58 * the specified location. 59 * <li>Invoke <code>getImage</code> to get the cached reprensentation and 60 * draw the image yourself. This is primarly useful when you are not 61 * using <code>VolatileImage</code>. 124 int attempts = 0; 125 VolatileImage volatileImage = (image instanceof VolatileImage) 126 ? (VolatileImage) image 127 : null; 128 do { 129 boolean draw = false; 130 if (volatileImage != null) { 131 // See if we need to recreate the image 132 switch (volatileImage.validate(config)) { 133 case VolatileImage.IMAGE_INCOMPATIBLE: 134 volatileImage.flush(); 135 image = null; 136 break; 137 case VolatileImage.IMAGE_RESTORED: 138 draw = true; 139 break; 140 } 141 } 142 if (image == null) { 143 // Recreate the image 144 if( config != null && (w != baseHeight || h != baseWidth)) { 145 AffineTransform tx = config.getDefaultTransform(); 146 double sx = tx.getScaleX(); 147 double sy = tx.getScaleY(); 148 if ( Double.compare(sx, 1) != 0 || 149 Double.compare(sy, 1) != 0) { 150 if (Math.abs(sx * baseWidth - w) < 1 && 151 Math.abs(sy * baseHeight - h) < 1) { 152 w = baseWidth; 153 h = baseHeight; 154 } else { 155 w = (int)Math.ceil(w / sx); 156 h = (int)Math.ceil(w / sy); 157 } 158 } 159 } 160 image = createImage(c, w, h, config, args); 161 cache.setImage(key, config, w, h, args, image); 162 draw = true; 163 volatileImage = (image instanceof VolatileImage) 164 ? (VolatileImage) image 165 : null; 166 } 167 if (draw) { 168 // Render to the Image 169 Graphics2D g2 = (Graphics2D) image.getGraphics(); 170 if (volatileImage == null) { 171 if ((w != baseWidth || h != baseHeight)) { 172 g2.scale((double) w / baseWidth, 173 (double) h / baseHeight); 174 } 175 paintToImage(c, image, g2, baseWidth, baseHeight, args); 176 } else { 177 SurfaceData sd = SurfaceManager.getManager(volatileImage) 178 .getPrimarySurfaceData(); 179 double sx = sd.getDefaultScaleX(); 180 double sy = sd.getDefaultScaleY(); 181 if ( Double.compare(sx, 1) != 0 || 182 Double.compare(sy, 1) != 0) { 183 g2.scale(1 / sx, 1 / sy); 184 } 185 paintToImage(c, image, g2, (int)Math.ceil(w * sx), 186 (int)Math.ceil(h * sy), args); 187 } 188 g2.dispose(); 189 } 190 191 // If we did this 3 times and the contents are still lost 192 // assume we're painting to a VolatileImage that is bogus and 193 // give up. Presumably we'll be called again to paint. 194 } while ((volatileImage != null) && 195 volatileImage.contentsLost() && ++attempts < 3); 196 197 return image; 198 } 199 200 private void paint0(Component c, Graphics g, int x, 201 int y, int w, int h, Object... args) { 202 Object key = getClass(); 203 GraphicsConfiguration config = getGraphicsConfiguration(c); 204 ImageCache cache = getCache(key); 205 Image image = cache.getImage(key, config, w, h, args); 206 207 if (image == null) { |