FileDocCategorySizeDatePackage
MultipleGradientPaint.javaAPI DocJava SE 6 API9535Tue Jun 10 00:25:18 BST 2008java.awt

MultipleGradientPaint

public abstract class MultipleGradientPaint extends Object implements Paint
This is the superclass for Paints which use a multiple color gradient to fill in their raster. It provides storage for variables and enumerated values common to {@code LinearGradientPaint} and {@code RadialGradientPaint}.
author
Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
since
1.6

Fields Summary
final int
transparency
The transparency of this paint object.
final float[]
fractions
Gradient keyframe values in the range 0 to 1.
final Color[]
colors
Gradient colors.
final AffineTransform
gradientTransform
Transform to apply to gradient.
final CycleMethod
cycleMethod
The method to use when painting outside the gradient bounds.
final ColorSpaceType
colorSpace
The color space in which to perform the gradient interpolation.
ColorModel
model
The following fields are used only by MultipleGradientPaintContext to cache certain values that remain constant and do not need to be recalculated for each context created from this paint instance.
float[]
normalizedIntervals
boolean
isSimpleLookup
SoftReference
gradients
SoftReference
gradient
int
fastGradientArraySize
Constructors Summary
MultipleGradientPaint(float[] fractions, Color[] colors, CycleMethod cycleMethod, ColorSpaceType colorSpace, AffineTransform gradientTransform)
Package-private constructor.

param
fractions numbers ranging from 0.0 to 1.0 specifying the distribution of colors along the gradient
param
colors array of colors corresponding to each fractional value
param
cycleMethod either {@code NO_CYCLE}, {@code REFLECT}, or {@code REPEAT}
param
colorSpace which color space to use for interpolation, either {@code SRGB} or {@code LINEAR_RGB}
param
gradientTransform transform to apply to the gradient
throws
NullPointerException if {@code fractions} array is null, or {@code colors} array is null, or {@code gradientTransform} is null, or {@code cycleMethod} is null, or {@code colorSpace} is null
throws
IllegalArgumentException if {@code fractions.length != colors.length}, or {@code colors} is less than 2 in size, or a {@code fractions} value is less than 0.0 or greater than 1.0, or the {@code fractions} are not provided in strictly increasing order

        if (fractions == null) {
            throw new NullPointerException("Fractions array cannot be null");
        }

        if (colors == null) {
            throw new NullPointerException("Colors array cannot be null");
        }

        if (cycleMethod == null) {
            throw new NullPointerException("Cycle method cannot be null");
        }

        if (colorSpace == null) {
            throw new NullPointerException("Color space cannot be null");
        }

        if (gradientTransform == null) {
            throw new NullPointerException("Gradient transform cannot be "+
                                           "null");
        }

        if (fractions.length != colors.length) {
            throw new IllegalArgumentException("Colors and fractions must " +
                                               "have equal size");
        }

        if (colors.length < 2) {
            throw new IllegalArgumentException("User must specify at least " +
                                               "2 colors");
        }

        // check that values are in the proper range and progress
        // in increasing order from 0 to 1
        float previousFraction = -1.0f;
        for (float currentFraction : fractions) {
            if (currentFraction < 0f || currentFraction > 1f) {
                throw new IllegalArgumentException("Fraction values must " +
                                                   "be in the range 0 to 1: " +
                                                   currentFraction);
            }

            if (currentFraction <= previousFraction) {
                throw new IllegalArgumentException("Keyframe fractions " +
                                                   "must be increasing: " +
                                                   currentFraction);
            }

            previousFraction = currentFraction;
        }

        // We have to deal with the cases where the first gradient stop is not
        // equal to 0 and/or the last gradient stop is not equal to 1.
        // In both cases, create a new point and replicate the previous
        // extreme point's color.
        boolean fixFirst = false;
        boolean fixLast = false;
        int len = fractions.length;
        int off = 0;

        if (fractions[0] != 0f) {
            // first stop is not equal to zero, fix this condition
            fixFirst = true;
            len++;
            off++;
        }
        if (fractions[fractions.length-1] != 1f) {
            // last stop is not equal to one, fix this condition
            fixLast = true;
            len++;
        }

        this.fractions = new float[len];
        System.arraycopy(fractions, 0, this.fractions, off, fractions.length);
        this.colors = new Color[len];
        System.arraycopy(colors, 0, this.colors, off, colors.length);

        if (fixFirst) {
            this.fractions[0] = 0f;
            this.colors[0] = colors[0];
        }
        if (fixLast) {
            this.fractions[len-1] = 1f;
            this.colors[len-1] = colors[colors.length - 1];
        }

        // copy some flags
        this.colorSpace = colorSpace;
        this.cycleMethod = cycleMethod;

        // copy the gradient transform
        this.gradientTransform = new AffineTransform(gradientTransform);

        // determine transparency
        boolean opaque = true;
        for (int i = 0; i < colors.length; i++){
            opaque = opaque && (colors[i].getAlpha() == 0xff);
        }
        this.transparency = opaque ? OPAQUE : TRANSLUCENT;
    
Methods Summary
public final java.awt.MultipleGradientPaint$ColorSpaceTypegetColorSpace()
Returns the enumerated type which specifies color space for interpolation.

return
the enumerated type which specifies color space for interpolation

        return colorSpace;
    
public final java.awt.Color[]getColors()
Returns a copy of the array of colors used by this gradient. The first color maps to the first value in the fractions array, and the last color maps to the last value in the fractions array.

return
a copy of the array of colors used by this gradient

        return Arrays.copyOf(colors, colors.length);
    
public final java.awt.MultipleGradientPaint$CycleMethodgetCycleMethod()
Returns the enumerated type which specifies cycling behavior.

return
the enumerated type which specifies cycling behavior

        return cycleMethod;
    
public final float[]getFractions()
Returns a copy of the array of floats used by this gradient to calculate color distribution. The returned array always has 0 as its first value and 1 as its last value, with increasing values in between.

return
a copy of the array of floats used by this gradient to calculate color distribution

        return Arrays.copyOf(fractions, fractions.length);
    
public final java.awt.geom.AffineTransformgetTransform()
Returns a copy of the transform applied to the gradient.

return
a copy of the transform applied to the gradient

        return new AffineTransform(gradientTransform);
    
public final intgetTransparency()
Returns the transparency mode for this Paint object.

return
an integer value representing the transparency mode for this Paint object
see
java.awt.Transparency

        return transparency;