jdk/src/share/classes/java/awt/MultipleGradientPaint.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8753 1d3d51072dad
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8753
diff changeset
     2
 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This is the superclass for Paints which use a multiple color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * gradient to fill in their raster.  It provides storage for variables and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * enumerated values common to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@code LinearGradientPaint} and {@code RadialGradientPaint}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class MultipleGradientPaint implements Paint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /** The method to use when painting outside the gradient bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static enum CycleMethod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
         * Use the terminal colors to fill the remaining area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        NO_CYCLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         * Cycle the gradient colors start-to-end, end-to-start
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * to fill the remaining area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        REFLECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * Cycle the gradient colors start-to-end, start-to-end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         * to fill the remaining area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        REPEAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** The color space in which to perform the gradient interpolation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static enum ColorSpaceType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * Indicates that the color interpolation should occur in sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SRGB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * Indicates that the color interpolation should occur in linearized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * RGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        LINEAR_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** The transparency of this paint object. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    final int transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** Gradient keyframe values in the range 0 to 1. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    final float[] fractions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Gradient colors. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    final Color[] colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** Transform to apply to gradient. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    final AffineTransform gradientTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** The method to use when painting outside the gradient bounds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    final CycleMethod cycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /** The color space in which to perform the gradient interpolation. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    final ColorSpaceType colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The following fields are used only by MultipleGradientPaintContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * to cache certain values that remain constant and do not need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * recalculated for each context created from this paint instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    ColorModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    float[] normalizedIntervals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    boolean isSimpleLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    SoftReference<int[][]> gradients;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    SoftReference<int[]> gradient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int fastGradientArraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Package-private constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param colorSpace which color space to use for interpolation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *                   either {@code SRGB} or {@code LINEAR_RGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param gradientTransform transform to apply to the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * if {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * or {@code gradientTransform} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * or {@code cycleMethod} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * or {@code colorSpace} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * if {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    MultipleGradientPaint(float[] fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                          Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                          CycleMethod cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                          ColorSpaceType colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                          AffineTransform gradientTransform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (fractions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new NullPointerException("Fractions array cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (colors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new NullPointerException("Colors array cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (cycleMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new NullPointerException("Cycle method cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (colorSpace == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new NullPointerException("Color space cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (gradientTransform == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NullPointerException("Gradient transform cannot be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                           "null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (fractions.length != colors.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new IllegalArgumentException("Colors and fractions must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                               "have equal size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (colors.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalArgumentException("User must specify at least " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                               "2 colors");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // check that values are in the proper range and progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // in increasing order from 0 to 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        float previousFraction = -1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (float currentFraction : fractions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (currentFraction < 0f || currentFraction > 1f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new IllegalArgumentException("Fraction values must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                                   "be in the range 0 to 1: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                                   currentFraction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (currentFraction <= previousFraction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new IllegalArgumentException("Keyframe fractions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                                   "must be increasing: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                   currentFraction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            previousFraction = currentFraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // We have to deal with the cases where the first gradient stop is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // equal to 0 and/or the last gradient stop is not equal to 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // In both cases, create a new point and replicate the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // extreme point's color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        boolean fixFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        boolean fixLast = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int len = fractions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (fractions[0] != 0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // first stop is not equal to zero, fix this condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            fixFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            off++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (fractions[fractions.length-1] != 1f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // last stop is not equal to one, fix this condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            fixLast = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.fractions = new float[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        System.arraycopy(fractions, 0, this.fractions, off, fractions.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.colors = new Color[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        System.arraycopy(colors, 0, this.colors, off, colors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (fixFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            this.fractions[0] = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            this.colors[0] = colors[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (fixLast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            this.fractions[len-1] = 1f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            this.colors[len-1] = colors[colors.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // copy some flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this.colorSpace = colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        this.cycleMethod = cycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // copy the gradient transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this.gradientTransform = new AffineTransform(gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // determine transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        boolean opaque = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (int i = 0; i < colors.length; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            opaque = opaque && (colors[i].getAlpha() == 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.transparency = opaque ? OPAQUE : TRANSLUCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns a copy of the array of floats used by this gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * to calculate color distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * The returned array always has 0 as its first value and 1 as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * last value, with increasing values in between.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return a copy of the array of floats used by this gradient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * calculate color distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public final float[] getFractions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return Arrays.copyOf(fractions, fractions.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns a copy of the array of colors used by this gradient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * The first color maps to the first value in the fractions array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * and the last color maps to the last value in the fractions array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return a copy of the array of colors used by this gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public final Color[] getColors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return Arrays.copyOf(colors, colors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Returns the enumerated type which specifies cycling behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return the enumerated type which specifies cycling behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public final CycleMethod getCycleMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return cycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns the enumerated type which specifies color space for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * interpolation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the enumerated type which specifies color space for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * interpolation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public final ColorSpaceType getColorSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns a copy of the transform applied to the gradient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
8753
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   289
     * <p>
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   290
     * Note that if no transform is applied to the gradient
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   291
     * when it is created, the identity transform is used.
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   292
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return a copy of the transform applied to the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public final AffineTransform getTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return new AffineTransform(gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
8753
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   300
     * Returns the transparency mode for this {@code Paint} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
8753
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   302
     * @return {@code OPAQUE} if all colors used by this
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   303
     *         {@code Paint} object are opaque,
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   304
     *         {@code TRANSLUCENT} if at least one of the
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 5506
diff changeset
   305
     *         colors used by this {@code Paint} object is not opaque.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see java.awt.Transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public final int getTransparency() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}