jdk/src/share/classes/java/awt/LinearGradientPaint.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8753 1d3d51072dad
child 20451 4cedf4e1560a
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: 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
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     Point2D start = new Point2D.Float(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     Point2D end = new Point2D.Float(50, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     float[] dist = {0.0f, 0.2f, 1.0f};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     Color[] colors = {Color.RED, Color.WHITE, Color.BLUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     LinearGradientPaint p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         new LinearGradientPaint(start, end, dist, colors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * This code will create a {@code LinearGradientPaint} which interpolates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * between red and white for the first 20% of the gradient and between white
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * and blue for the remaining 80%.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * This image demonstrates the example code above for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * of the three cycle methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <img src = "doc-files/LinearGradientPaint.png">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * </center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @see java.awt.Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @see java.awt.Graphics2D#setPaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public final class LinearGradientPaint extends MultipleGradientPaint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /** Gradient start and end points. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private final Point2D start, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a {@code LinearGradientPaint} with a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * {@code NO_CYCLE} repeating method and {@code SRGB} color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param startX the X coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param startY the Y coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param endX   the X coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param endY   the Y coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * if {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * or {@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
    public LinearGradientPaint(float startX, float startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                               float endX, float endY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                               float[] fractions, Color[] colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructs a {@code LinearGradientPaint} with a default {@code SRGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param startX the X coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param startY the Y coordinate of the gradient axis start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param endX   the X coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param endY   the Y coordinate of the gradient axis end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *               in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * if {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * or {@code cycleMethod} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public LinearGradientPaint(float startX, float startY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                               float endX, float endY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                               CycleMethod cycleMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             cycleMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Constructs a {@code LinearGradientPaint} with a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * {@code NO_CYCLE} repeating method and {@code SRGB} color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * or {@code colors} array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                               float[] fractions, Color[] colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this(start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Constructs a {@code LinearGradientPaint} with a default {@code SRGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * or {@code cycleMethod} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                               CycleMethod cycleMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        this(start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
             fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             ColorSpaceType.SRGB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             new AffineTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Constructs a {@code LinearGradientPaint}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param start the gradient axis start {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param end the gradient axis end {@code Point2D} in user space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param fractions numbers ranging from 0.0 to 1.0 specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                  distribution of colors along the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param colors array of colors corresponding to each fractional value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *                    or {@code REPEAT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param colorSpace which color space to use for interpolation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *                   either {@code SRGB} or {@code LINEAR_RGB}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param gradientTransform transform to apply to the gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * if one of the points is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * or {@code fractions} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * or {@code colors} array is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * or {@code cycleMethod} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * or {@code colorSpace} is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * or {@code gradientTransform} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * if start and end points are the same points,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * or {@code fractions.length != colors.length},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * or {@code colors} is less than 2 in size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * or a {@code fractions} value is less than 0.0 or greater than 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * or the {@code fractions} are not provided in strictly increasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
7006
05505fff1342 4358979: javax.swing.border should have a DashedBorder
malenkov
parents: 5506
diff changeset
   280
    @ConstructorProperties({ "startPoint", "endPoint", "fractions", "colors", "cycleMethod", "colorSpace", "transform" })
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public LinearGradientPaint(Point2D start, Point2D end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                               float[] fractions, Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                               CycleMethod cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                               ColorSpaceType colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                               AffineTransform gradientTransform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // check input parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (start == null || end == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new NullPointerException("Start and end points must be" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                           "non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (start.equals(end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new IllegalArgumentException("Start point cannot equal" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                               "endpoint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // copy the points...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        this.start = new Point2D.Double(start.getX(), start.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        this.end = new Point2D.Double(end.getX(), end.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   306
     * 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
   307
     * 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
   308
     * 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
   309
     * 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
   310
     * 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
   311
     *
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   312
     * @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
   313
     *           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
   314
     *           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
   315
     * @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
   316
     *                     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
   317
     * @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
   318
     *                   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
   319
     * @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
   320
     *              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
   321
     * @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
   322
     *              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
   323
     * @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
   324
     *         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
   325
     * @see Paint
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   326
     * @see PaintContext
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   327
     * @see ColorModel
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   328
     * @see Rectangle
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   329
     * @see Rectangle2D
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   330
     * @see AffineTransform
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   331
     * @see RenderingHints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public PaintContext createContext(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                      Rectangle deviceBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                      Rectangle2D userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                      AffineTransform transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                      RenderingHints hints)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // avoid modifying the user's transform...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        transform = new AffineTransform(transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // incorporate the gradient transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        transform.concatenate(gradientTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if ((fractions.length == 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            (cycleMethod != CycleMethod.REPEAT) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            (colorSpace == ColorSpaceType.SRGB))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // faster to use the basic GradientPaintContext for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // common case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return new GradientPaintContext(cm, start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                            transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                            colors[0], colors[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                            cyclic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return new LinearGradientPaintContext(this, cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                                  deviceBounds, userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                                  transform, hints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                  start, end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                                  fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                                  cycleMethod, colorSpace);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Returns a copy of the start point of the gradient axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return a {@code Point2D} object that is a copy of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * that anchors the first color of this {@code LinearGradientPaint}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public Point2D getStartPoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return new Point2D.Double(start.getX(), start.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Returns a copy of the end point of the gradient axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return a {@code Point2D} object that is a copy of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * that anchors the last color of this {@code LinearGradientPaint}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public Point2D getEndPoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return new Point2D.Double(end.getX(), end.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
}