src/macosx/classes/com/apple/laf/AquaPainter.java

Print this page




  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 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.util.HashMap;
  31 
  32 import com.apple.laf.AquaImageFactory.RecyclableSlicedImageControl;
  33 import com.apple.laf.AquaImageFactory.NineSliceMetrics;
  34 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  35 
  36 import sun.awt.image.*;
  37 import sun.java2d.*;
  38 import sun.print.*;
  39 import apple.laf.*;
  40 import apple.laf.JRSUIUtils.NineSliceMetricsProvider;

  41 
  42 abstract class AquaPainter <T extends JRSUIState> {
  43     static <T extends JRSUIState> AquaPainter<T> create(final T state) {
  44         return new AquaSingleImagePainter<>(state);
  45     }
  46 
  47     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
  48         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
  49     }
  50 
  51     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {
  52         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, true, true);
  53     }
  54 
  55     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {
  56         return create(state, new NineSliceMetricsProvider() {
  57             @Override
  58                public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state) {
  59                 return new NineSliceMetrics(minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically);
  60             }


 138         @Override
 139         void paint(final Graphics2D g, final T stateToPaint) {
 140             paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 141         }
 142 
 143         static void paintFromSingleCachedImage(final Graphics2D g,
 144                 final JRSUIControl control, final JRSUIState controlState,
 145                 final Rectangle bounds) {
 146             if (bounds.width <= 0 || bounds.height <= 0) {
 147                 return;
 148             }
 149 
 150             int scale = 1;
 151             if (g instanceof SunGraphics2D) {
 152                 scale = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 153             }
 154             final GraphicsConfiguration config = g.getDeviceConfiguration();
 155             final ImageCache cache = ImageCache.getInstance();
 156             final int imgW = bounds.width * scale;
 157             final int imgH = bounds.height * scale;
 158             BufferedImage img = (BufferedImage) cache.getImage(config, imgW, imgH, scale, controlState);


 159             if (img == null) {
 160                 img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB_PRE);
 161                 cache.setImage(img, config, imgW, imgH, scale, controlState);
 162                 final WritableRaster raster = img.getRaster();
 163                 final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
 164 
 165                 control.set(controlState);
 166                 control.paint(SunWritableRaster.stealData(buffer, 0),
 167                         imgW, imgH, 0, 0, bounds.width, bounds.height);
 168                 SunWritableRaster.markDirty(buffer);
 169             }
 170 
 171             g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height, null);
 172         }
 173     }
 174 





















































 175     private static class RecyclableJRSUISlicedImageControl
 176             extends RecyclableSlicedImageControl {
 177 
 178         private final JRSUIControl control;
 179         private final JRSUIState state;
 180 
 181         RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
 182             super(metrics);
 183             this.control = control;
 184             this.state = state;
 185         }
 186 
 187         @Override
 188         protected Image createTemplateImage(int width, int height) {
 189             BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);
 190 
 191             final WritableRaster raster = image.getRaster();
 192             final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 193 
 194             control.set(state);




  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 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.util.HashMap;
  31 
  32 import com.apple.laf.AquaImageFactory.RecyclableSlicedImageControl;
  33 import com.apple.laf.AquaImageFactory.NineSliceMetrics;
  34 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  35 
  36 import sun.awt.image.*;
  37 import sun.java2d.*;
  38 import sun.print.*;
  39 import apple.laf.*;
  40 import apple.laf.JRSUIUtils.NineSliceMetricsProvider;
  41 import sun.awt.image.ImageCache;
  42 
  43 abstract class AquaPainter <T extends JRSUIState> {
  44     static <T extends JRSUIState> AquaPainter<T> create(final T state) {
  45         return new AquaSingleImagePainter<>(state);
  46     }
  47 
  48     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
  49         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
  50     }
  51 
  52     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {
  53         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, true, true);
  54     }
  55 
  56     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {
  57         return create(state, new NineSliceMetricsProvider() {
  58             @Override
  59                public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state) {
  60                 return new NineSliceMetrics(minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically);
  61             }


 139         @Override
 140         void paint(final Graphics2D g, final T stateToPaint) {
 141             paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 142         }
 143 
 144         static void paintFromSingleCachedImage(final Graphics2D g,
 145                 final JRSUIControl control, final JRSUIState controlState,
 146                 final Rectangle bounds) {
 147             if (bounds.width <= 0 || bounds.height <= 0) {
 148                 return;
 149             }
 150 
 151             int scale = 1;
 152             if (g instanceof SunGraphics2D) {
 153                 scale = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 154             }
 155             final GraphicsConfiguration config = g.getDeviceConfiguration();
 156             final ImageCache cache = ImageCache.getInstance();
 157             final int imgW = bounds.width * scale;
 158             final int imgH = bounds.height * scale;
 159             AquaPixelsKey key = new AquaPixelsKey(config,
 160                     imgW, imgH, scale, controlState);
 161             BufferedImage img = (BufferedImage) cache.getImage(key);
 162             if (img == null) {
 163                 img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB_PRE);
 164                 cache.setImage(key, img);
 165                 final WritableRaster raster = img.getRaster();
 166                 final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
 167 
 168                 control.set(controlState);
 169                 control.paint(SunWritableRaster.stealData(buffer, 0),
 170                         imgW, imgH, 0, 0, bounds.width, bounds.height);
 171                 SunWritableRaster.markDirty(buffer);
 172             }
 173 
 174             g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height, null);
 175         }
 176     }
 177 
 178     private static class AquaPixelsKey implements ImageCache.PixelsKey {
 179 
 180         private final int pixelCount;
 181         private final int hash;
 182 
 183         // key parts
 184         private final GraphicsConfiguration config;
 185         private final int w;
 186         private final int h;
 187         private final int scale;
 188         private final JRSUIState state;
 189 
 190         AquaPixelsKey(final GraphicsConfiguration config,
 191                 final int w, final int h, final int scale,
 192                 final JRSUIState state) {
 193             this.pixelCount = w * h;
 194             this.config = config;
 195             this.w = w;
 196             this.h = h;
 197             this.scale = scale;
 198             this.state = state;
 199             this.hash = hash();
 200         }
 201 
 202         public int getPixelCount() {
 203             return pixelCount;
 204         }
 205 
 206         private int hash() {
 207             int hash = config != null ? config.hashCode() : 0;
 208             hash = 31 * hash + w;
 209             hash = 31 * hash + h;
 210             hash = 31 * hash + scale;
 211             hash = 31 * hash + state.hashCode();
 212             return hash;
 213         }
 214 
 215         @Override
 216         public int hashCode() {
 217             return hash;
 218         }
 219 
 220         @Override
 221         public boolean equals(Object obj) {
 222             if (obj instanceof AquaPixelsKey) {
 223                 AquaPixelsKey key = (AquaPixelsKey) obj;
 224                 return config == key.config && w == key.w && h == key.h
 225                         && scale == key.scale && state.equals(key.state);
 226             }
 227             return false;
 228         }
 229     }
 230 
 231     private static class RecyclableJRSUISlicedImageControl
 232             extends RecyclableSlicedImageControl {
 233 
 234         private final JRSUIControl control;
 235         private final JRSUIState state;
 236 
 237         RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
 238             super(metrics);
 239             this.control = control;
 240             this.state = state;
 241         }
 242 
 243         @Override
 244         protected Image createTemplateImage(int width, int height) {
 245             BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);
 246 
 247             final WritableRaster raster = image.getRaster();
 248             final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 249 
 250             control.set(state);