jdk/src/share/classes/java/awt/LinearGradientPaint.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 23010 6dadb192ad81
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21957
diff changeset
     2
 * Copyright (c) 2006, 2013, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ColorModel;
7006
05505fff1342 4358979: javax.swing.border should have a DashedBorder
malenkov
parents: 5506
diff changeset
    32
import java.beans.ConstructorProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The {@code LinearGradientPaint} class provides a way to fill
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a {@link java.awt.Shape} with a linear color gradient pattern.  The user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * may specify two or more gradient colors, and this paint will provide an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * interpolation between each color.  The user also specifies start and end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * points which define where in user space the color gradient should begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The user must provide an array of floats specifying how to distribute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * colors along the gradient.  These values should range from 0.0 to 1.0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * act like keyframes along the gradient (they mark where the gradient should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * be exactly a particular color).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * In the event that the user does not set the first keyframe value equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to 0 and/or the last keyframe value equal to 1, keyframes will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * at these positions and the first and last colors will be replicated there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * So, if a user specifies the following arrays to construct a gradient:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     {Color.BLUE, Color.RED}, {.3f, .7f}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * this will be converted to a gradient with the following keyframes:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     {Color.BLUE, Color.BLUE, Color.RED, Color.RED}, {0f, .3f, .7f, 1f}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
8753
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    60
 * The user may also select what action the {@code LinearGradientPaint} object
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    61
 * takes when it is filling the space outside the start and end points by
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    62
 * setting {@code CycleMethod} to either {@code REFLECTION} or {@code REPEAT}.
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    63
 * The distances between any two colors in any of the reflected or repeated
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    64
 * copies of the gradient are the same as the distance between those same two
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    65
 * colors between the start and end points.
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    66
 * Note that some minor variations in distances may occur due to sampling at
1d3d51072dad 7022931: GradientPaint class spec clarification: 7022931, 7016391, 7017246, 7019386
dav
parents: 7006
diff changeset
    67
 * the granularity of a pixel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * If no cycle method is specified, {@code NO_CYCLE} will be chosen by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * default, which means the endpoint colors will be used to fill the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * remaining area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * The colorSpace parameter allows the user to specify in which colorspace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the interpolation should be performed, default sRGB or linearized RGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The following code demonstrates typical usage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * {@code LinearGradientPaint}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     Point2D start = new Point2D.Float(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     Point2D end = new Point2D.Float(50, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     float[] dist = {0.0f, 0.2f, 1.0f};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     Color[] colors = {Color.RED, Color.WHITE, Color.BLUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     LinearGradientPaint p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *         new LinearGradientPaint(start, end, dist, colors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * This code will create a {@code LinearGradientPaint} which interpolates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * between red and white for the first 20% of the gradient and between white
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * and blue for the remaining 80%.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * This image demonstrates the example code above for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * of the three cycle methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <center>
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 20451
diff changeset
    95
 * <img src = "doc-files/LinearGradientPaint.png"
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 20451
diff changeset
    96
 * alt="image showing the output of the example code">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * </center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see java.awt.Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @see java.awt.Graphics2D#setPaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
public final class LinearGradientPaint extends MultipleGradientPaint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Gradient start and end points. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private final Point2D start, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Constructs a {@code LinearGradientPaint} with a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * {@code NO_CYCLE} repeating method and {@code SRGB} color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param startX the X coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param startY the Y coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param endX   the X coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param endY   the Y coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * if {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public LinearGradientPaint(float startX, float startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               float endX, float endY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                               float[] fractions, Color[] colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Constructs a {@code LinearGradientPaint} with a default {@code SRGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param startX the X coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param startY the Y coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param endX   the X coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param endY   the Y coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * if {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * or {@code cycleMethod} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public LinearGradientPaint(float startX, float startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                               float endX, float endY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                               CycleMethod cycleMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        this(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             cycleMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Constructs a {@code LinearGradientPaint} with a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * {@code NO_CYCLE} repeating method and {@code SRGB} color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * or {@code colors} array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                               float[] fractions, Color[] colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this(start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Constructs a {@code LinearGradientPaint} with a default {@code SRGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * or {@code cycleMethod} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               CycleMethod cycleMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this(start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
             fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
             cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             ColorSpaceType.SRGB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             new AffineTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Constructs a {@code LinearGradientPaint}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param colorSpace which color space to use for interpolation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *                   either {@code SRGB} or {@code LINEAR_RGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param gradientTransform transform to apply to the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * or {@code cycleMethod} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * or {@code colorSpace} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * or {@code gradientTransform} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
7006
05505fff1342 4358979: javax.swing.border should have a DashedBorder
malenkov
parents: 5506
diff changeset
   279
    @ConstructorProperties({ "startPoint", "endPoint", "fractions", "colors", "cycleMethod", "colorSpace", "transform" })
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                               CycleMethod cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                               ColorSpaceType colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                               AffineTransform gradientTransform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // check input parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (start == null || end == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new NullPointerException("Start and end points must be" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                           "non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (start.equals(end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new IllegalArgumentException("Start point cannot equal" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                               "endpoint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // copy the points...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        this.start = new Point2D.Double(start.getX(), start.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        this.end = new Point2D.Double(end.getX(), end.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   305
     * Creates and returns a {@link PaintContext} used to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   306
     * generate a linear color gradient pattern.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   307
     * See the {@link Paint#createContext specification} of the
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   308
     * method in the {@link Paint} interface for information
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   309
     * on null parameter handling.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   310
     *
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   311
     * @param cm the preferred {@link ColorModel} which represents the most convenient
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   312
     *           format for the caller to receive the pixel data, or {@code null}
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   313
     *           if there is no preference.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   314
     * @param deviceBounds the device space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   315
     *                     of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   316
     * @param userBounds the user space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   317
     *                   of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   318
     * @param transform the {@link AffineTransform} from user
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   319
     *              space into device space.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   320
     * @param hints the set of hints that the context object can use to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   321
     *              choose between rendering alternatives.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   322
     * @return the {@code PaintContext} for
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   323
     *         generating color patterns.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   324
     * @see Paint
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   325
     * @see PaintContext
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   326
     * @see ColorModel
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   327
     * @see Rectangle
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   328
     * @see Rectangle2D
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   329
     * @see AffineTransform
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   330
     * @see RenderingHints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public PaintContext createContext(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                      Rectangle deviceBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                      Rectangle2D userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                      AffineTransform transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                      RenderingHints hints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // avoid modifying the user's transform...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        transform = new AffineTransform(transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // incorporate the gradient transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        transform.concatenate(gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if ((fractions.length == 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            (cycleMethod != CycleMethod.REPEAT) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            (colorSpace == ColorSpaceType.SRGB))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // faster to use the basic GradientPaintContext for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // common case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return new GradientPaintContext(cm, start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                            transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                            colors[0], colors[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                            cyclic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return new LinearGradientPaintContext(this, cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                                  deviceBounds, userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                                  transform, hints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                                  start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                  fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                                  cycleMethod, colorSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Returns a copy of the start point of the gradient axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return a {@code Point2D} object that is a copy of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * that anchors the first color of this {@code LinearGradientPaint}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public Point2D getStartPoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return new Point2D.Double(start.getX(), start.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns a copy of the end point of the gradient axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return a {@code Point2D} object that is a copy of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * that anchors the last color of this {@code LinearGradientPaint}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public Point2D getEndPoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return new Point2D.Double(end.getX(), end.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}