src/java.desktop/share/classes/java/awt/RadialGradientPaintContext.java

Print this page




  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 
  26 package java.awt;
  27 
  28 import java.awt.MultipleGradientPaint.CycleMethod;
  29 import java.awt.MultipleGradientPaint.ColorSpaceType;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.geom.Rectangle2D;
  32 import java.awt.image.ColorModel;
  33 
  34 /**
  35  * Provides the actual implementation for the RadialGradientPaint.
  36  * This is where the pixel processing is done.  A RadialGradienPaint
  37  * only supports circular gradients, but it should be possible to scale
  38  * the circle to look approximately elliptical, by means of a
  39  * gradient transform passed into the RadialGradientPaint constructor.
  40  *
  41  * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
  42  */
  43 final class RadialGradientPaintContext extends MultipleGradientPaintContext {
  44 
  45     /** True when (focus == center).  */
  46     private boolean isSimpleFocus = false;
  47 
  48     /** True when (cycleMethod == NO_CYCLE). */
  49     private boolean isNonCyclic = false;
  50 
  51     /** Radius of the outermost circle defining the 100% gradient stop. */
  52     private float radius;
  53 
  54     /** Variables representing center and focus points. */
  55     private float centerX, centerY, focusX, focusY;
  56 


 116                                float r,
 117                                float fx, float fy,
 118                                float[] fractions,
 119                                Color[] colors,
 120                                CycleMethod cycleMethod,
 121                                ColorSpaceType colorSpace)
 122     {
 123         super(paint, cm, deviceBounds, userBounds, t, hints,
 124               fractions, colors, cycleMethod, colorSpace);
 125 
 126         // copy some parameters
 127         centerX = cx;
 128         centerY = cy;
 129         focusX = fx;
 130         focusY = fy;
 131         radius = r;
 132 
 133         this.isSimpleFocus = (focusX == centerX) && (focusY == centerY);
 134         this.isNonCyclic = (cycleMethod == CycleMethod.NO_CYCLE);
 135 
 136         // for use in the quadractic equation
 137         radiusSq = radius * radius;
 138 
 139         float dX = focusX - centerX;
 140         float dY = focusY - centerY;
 141 
 142         double distSq = (dX * dX) + (dY * dY);
 143 
 144         // test if distance from focus to center is greater than the radius
 145         if (distSq > radiusSq * SCALEBACK) {
 146             // clamp focus to radius
 147             float scalefactor = (float)Math.sqrt(radiusSq * SCALEBACK / distSq);
 148             dX = dX * scalefactor;
 149             dY = dY * scalefactor;
 150             focusX = centerX + dX;
 151             focusY = centerY + dY;
 152         }
 153 
 154         // calculate the solution to be used in the case where X == focusX
 155         // in cyclicCircularGradientFillRaster()
 156         trivial = (float)Math.sqrt(radiusSq - (dX * dX));




  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 
  26 package java.awt;
  27 
  28 import java.awt.MultipleGradientPaint.CycleMethod;
  29 import java.awt.MultipleGradientPaint.ColorSpaceType;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.geom.Rectangle2D;
  32 import java.awt.image.ColorModel;
  33 
  34 /**
  35  * Provides the actual implementation for the RadialGradientPaint.
  36  * This is where the pixel processing is done.  A RadialGradientPaint
  37  * only supports circular gradients, but it should be possible to scale
  38  * the circle to look approximately elliptical, by means of a
  39  * gradient transform passed into the RadialGradientPaint constructor.
  40  *
  41  * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
  42  */
  43 final class RadialGradientPaintContext extends MultipleGradientPaintContext {
  44 
  45     /** True when (focus == center).  */
  46     private boolean isSimpleFocus = false;
  47 
  48     /** True when (cycleMethod == NO_CYCLE). */
  49     private boolean isNonCyclic = false;
  50 
  51     /** Radius of the outermost circle defining the 100% gradient stop. */
  52     private float radius;
  53 
  54     /** Variables representing center and focus points. */
  55     private float centerX, centerY, focusX, focusY;
  56 


 116                                float r,
 117                                float fx, float fy,
 118                                float[] fractions,
 119                                Color[] colors,
 120                                CycleMethod cycleMethod,
 121                                ColorSpaceType colorSpace)
 122     {
 123         super(paint, cm, deviceBounds, userBounds, t, hints,
 124               fractions, colors, cycleMethod, colorSpace);
 125 
 126         // copy some parameters
 127         centerX = cx;
 128         centerY = cy;
 129         focusX = fx;
 130         focusY = fy;
 131         radius = r;
 132 
 133         this.isSimpleFocus = (focusX == centerX) && (focusY == centerY);
 134         this.isNonCyclic = (cycleMethod == CycleMethod.NO_CYCLE);
 135 
 136         // for use in the quadratic equation
 137         radiusSq = radius * radius;
 138 
 139         float dX = focusX - centerX;
 140         float dY = focusY - centerY;
 141 
 142         double distSq = (dX * dX) + (dY * dY);
 143 
 144         // test if distance from focus to center is greater than the radius
 145         if (distSq > radiusSq * SCALEBACK) {
 146             // clamp focus to radius
 147             float scalefactor = (float)Math.sqrt(radiusSq * SCALEBACK / distSq);
 148             dX = dX * scalefactor;
 149             dY = dY * scalefactor;
 150             focusX = centerX + dX;
 151             focusY = centerY + dY;
 152         }
 153 
 154         // calculate the solution to be used in the case where X == focusX
 155         // in cyclicCircularGradientFillRaster()
 156         trivial = (float)Math.sqrt(radiusSq - (dX * dX));