src/share/classes/java/awt/GradientPaintContext.java

Print this page




  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.image.Raster;
  29 import sun.awt.image.IntegerComponentRaster;
  30 import java.awt.image.ColorModel;
  31 import java.awt.image.DirectColorModel;
  32 import java.awt.geom.Point2D;
  33 import java.awt.geom.AffineTransform;
  34 import java.awt.geom.NoninvertibleTransformException;
  35 import java.lang.ref.WeakReference;
  36 
  37 class GradientPaintContext implements PaintContext {
  38     static ColorModel xrgbmodel =
  39         new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
  40     static ColorModel xbgrmodel =
  41         new DirectColorModel(24, 0x000000ff, 0x0000ff00, 0x00ff0000);
  42 
  43     static ColorModel cachedModel;
  44     static WeakReference cached;
  45 
  46     static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
  47         if (cm == cachedModel) {
  48             if (cached != null) {
  49                 Raster ras = (Raster) cached.get();
  50                 if (ras != null &&
  51                     ras.getWidth() >= w &&
  52                     ras.getHeight() >= h)
  53                 {
  54                     cached = null;
  55                     return ras;
  56                 }
  57             }
  58         }
  59         return cm.createCompatibleWritableRaster(w, h);
  60     }
  61 
  62     static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
  63         if (cached != null) {
  64             Raster cras = (Raster) cached.get();
  65             if (cras != null) {
  66                 int cw = cras.getWidth();
  67                 int ch = cras.getHeight();
  68                 int iw = ras.getWidth();
  69                 int ih = ras.getHeight();
  70                 if (cw >= iw && ch >= ih) {
  71                     return;
  72                 }
  73                 if (cw * ch >= iw * ih) {
  74                     return;
  75                 }
  76             }
  77         }
  78         cachedModel = cm;
  79         cached = new WeakReference(ras);
  80     }
  81 
  82     double x1;
  83     double y1;
  84     double dx;
  85     double dy;
  86     boolean cyclic;
  87     int interp[];
  88     Raster saved;
  89     ColorModel model;
  90 
  91     public GradientPaintContext(ColorModel cm,
  92                                 Point2D p1, Point2D p2, AffineTransform xform,
  93                                 Color c1, Color c2, boolean cyclic) {
  94         // First calculate the distance moved in user space when
  95         // we move a single unit along the X & Y axes in device space.
  96         Point2D xvec = new Point2D.Double(1, 0);
  97         Point2D yvec = new Point2D.Double(0, 1);
  98         try {
  99             AffineTransform inverse = xform.createInverse();




  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.image.Raster;
  29 import sun.awt.image.IntegerComponentRaster;
  30 import java.awt.image.ColorModel;
  31 import java.awt.image.DirectColorModel;
  32 import java.awt.geom.Point2D;
  33 import java.awt.geom.AffineTransform;
  34 import java.awt.geom.NoninvertibleTransformException;
  35 import java.lang.ref.WeakReference;
  36 
  37 class GradientPaintContext implements PaintContext {
  38     static ColorModel xrgbmodel =
  39         new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
  40     static ColorModel xbgrmodel =
  41         new DirectColorModel(24, 0x000000ff, 0x0000ff00, 0x00ff0000);
  42 
  43     static ColorModel cachedModel;
  44     static WeakReference<Raster> cached;
  45 
  46     static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
  47         if (cm == cachedModel) {
  48             if (cached != null) {
  49                 Raster ras = (Raster) cached.get();
  50                 if (ras != null &&
  51                     ras.getWidth() >= w &&
  52                     ras.getHeight() >= h)
  53                 {
  54                     cached = null;
  55                     return ras;
  56                 }
  57             }
  58         }
  59         return cm.createCompatibleWritableRaster(w, h);
  60     }
  61 
  62     static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
  63         if (cached != null) {
  64             Raster cras = (Raster) cached.get();
  65             if (cras != null) {
  66                 int cw = cras.getWidth();
  67                 int ch = cras.getHeight();
  68                 int iw = ras.getWidth();
  69                 int ih = ras.getHeight();
  70                 if (cw >= iw && ch >= ih) {
  71                     return;
  72                 }
  73                 if (cw * ch >= iw * ih) {
  74                     return;
  75                 }
  76             }
  77         }
  78         cachedModel = cm;
  79         cached = new WeakReference<>(ras);
  80     }
  81 
  82     double x1;
  83     double y1;
  84     double dx;
  85     double dy;
  86     boolean cyclic;
  87     int interp[];
  88     Raster saved;
  89     ColorModel model;
  90 
  91     public GradientPaintContext(ColorModel cm,
  92                                 Point2D p1, Point2D p2, AffineTransform xform,
  93                                 Color c1, Color c2, boolean cyclic) {
  94         // First calculate the distance moved in user space when
  95         // we move a single unit along the X & Y axes in device space.
  96         Point2D xvec = new Point2D.Double(1, 0);
  97         Point2D yvec = new Point2D.Double(0, 1);
  98         try {
  99             AffineTransform inverse = xform.createInverse();