LinearGradientPaint.java revision 213
0N/A * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * The {@code LinearGradientPaint} class provides a way to fill 0N/A * a {@link java.awt.Shape} with a linear color gradient pattern. The user 0N/A * may specify two or more gradient colors, and this paint will provide an 0N/A * interpolation between each color. The user also specifies start and end 0N/A * points which define where in user space the color gradient should begin 0N/A * The user must provide an array of floats specifying how to distribute the 0N/A * colors along the gradient. These values should range from 0.0 to 1.0 and 0N/A * act like keyframes along the gradient (they mark where the gradient should 0N/A * be exactly a particular color). 0N/A * In the event that the user does not set the first keyframe value equal 0N/A * to 0 and/or the last keyframe value equal to 1, keyframes will be created 0N/A * at these positions and the first and last colors will be replicated there. 0N/A * So, if a user specifies the following arrays to construct a gradient:<br> 0N/A * {Color.BLUE, Color.RED}, {.3f, .7f} 0N/A * this will be converted to a gradient with the following keyframes:<br> 0N/A * {Color.BLUE, Color.BLUE, Color.RED, Color.RED}, {0f, .3f, .7f, 1f} 0N/A * The user may also select what action the {@code LinearGradientPaint} 0N/A * should take when filling color outside the start and end points. 0N/A * If no cycle method is specified, {@code NO_CYCLE} will be chosen by 0N/A * default, which means the endpoint colors will be used to fill the 0N/A * The colorSpace parameter allows the user to specify in which colorspace 0N/A * the interpolation should be performed, default sRGB or linearized RGB. 0N/A * The following code demonstrates typical usage of 0N/A * {@code LinearGradientPaint}: 0N/A * Point2D start = new Point2D.Float(0, 0); 0N/A * Point2D end = new Point2D.Float(50, 50); 0N/A * float[] dist = {0.0f, 0.2f, 1.0f}; 0N/A * Color[] colors = {Color.RED, Color.WHITE, Color.BLUE}; 0N/A * LinearGradientPaint p = 0N/A * new LinearGradientPaint(start, end, dist, colors); 0N/A * This code will create a {@code LinearGradientPaint} which interpolates 0N/A * between red and white for the first 20% of the gradient and between white 0N/A * and blue for the remaining 80%. 0N/A * This image demonstrates the example code above for each 0N/A * of the three cycle methods: 0N/A * @see java.awt.Paint 0N/A * @see java.awt.Graphics2D#setPaint 0N/A * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans 0N/A /** Gradient start and end points. */ 0N/A * Constructs a {@code LinearGradientPaint} with a default 0N/A * {@code NO_CYCLE} repeating method and {@code SRGB} color space. 0N/A * @param startX the X coordinate of the gradient axis start point 0N/A * @param startY the Y coordinate of the gradient axis start point 0N/A * @param endX the X coordinate of the gradient axis end point 0N/A * @param endY the Y coordinate of the gradient axis end point 0N/A * @param fractions numbers ranging from 0.0 to 1.0 specifying the 0N/A * distribution of colors along the gradient 0N/A * @param colors array of colors corresponding to each fractional value 0N/A * @throws NullPointerException 0N/A * if {@code fractions} array is null, 0N/A * or {@code colors} array is null, 0N/A * @throws IllegalArgumentException 0N/A * if start and end points are the same points, 0N/A * or {@code fractions.length != colors.length}, 0N/A * or {@code colors} is less than 2 in size, 0N/A * or a {@code fractions} value is less than 0.0 or greater than 1.0, 0N/A * or the {@code fractions} are not provided in strictly increasing order 0N/A * Constructs a {@code LinearGradientPaint} with a default {@code SRGB} 0N/A * @param startX the X coordinate of the gradient axis start point 0N/A * @param startY the Y coordinate of the gradient axis start point 0N/A * @param endX the X coordinate of the gradient axis end point 0N/A * @param endY the Y coordinate of the gradient axis end point 0N/A * @param fractions numbers ranging from 0.0 to 1.0 specifying the 0N/A * distribution of colors along the gradient 0N/A * @param colors array of colors corresponding to each fractional value 0N/A * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT}, 0N/A * @throws NullPointerException 0N/A * if {@code fractions} array is null, 0N/A * or {@code colors} array is null, 0N/A * or {@code cycleMethod} is null 0N/A * @throws IllegalArgumentException 0N/A * if start and end points are the same points, 0N/A * or {@code fractions.length != colors.length}, 0N/A * or {@code colors} is less than 2 in size, 0N/A * or a {@code fractions} value is less than 0.0 or greater than 1.0, 0N/A * or the {@code fractions} are not provided in strictly increasing order 0N/A * Constructs a {@code LinearGradientPaint} with a default 0N/A * {@code NO_CYCLE} repeating method and {@code SRGB} color space. 0N/A * @param start the gradient axis start {@code Point2D} in user space 0N/A * @param end the gradient axis end {@code Point2D} in user space 0N/A * @param fractions numbers ranging from 0.0 to 1.0 specifying the 0N/A * distribution of colors along the gradient 0N/A * @param colors array of colors corresponding to each fractional value 0N/A * @throws NullPointerException 0N/A * if one of the points is null, 0N/A * or {@code fractions} array is null, 0N/A * or {@code colors} array is null 0N/A * @throws IllegalArgumentException 0N/A * if start and end points are the same points, 0N/A * or {@code fractions.length != colors.length}, 0N/A * or {@code colors} is less than 2 in size, 0N/A * or a {@code fractions} value is less than 0.0 or greater than 1.0, 0N/A * or the {@code fractions} are not provided in strictly increasing order 0N/A * Constructs a {@code LinearGradientPaint} with a default {@code SRGB} 0N/A * @param start the gradient axis start {@code Point2D} in user space 0N/A * @param end the gradient axis end {@code Point2D} in user space 0N/A * @param fractions numbers ranging from 0.0 to 1.0 specifying the 0N/A * distribution of colors along the gradient 0N/A * @param colors array of colors corresponding to each fractional value 0N/A * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT}, 0N/A * @throws NullPointerException 0N/A * if one of the points is null, 0N/A * or {@code fractions} array is null, 0N/A * or {@code colors} array is null, 0N/A * or {@code cycleMethod} is null 0N/A * @throws IllegalArgumentException 0N/A * if start and end points are the same points, 0N/A * or {@code fractions.length != colors.length}, 0N/A * or {@code colors} is less than 2 in size, 0N/A * or a {@code fractions} value is less than 0.0 or greater than 1.0, 0N/A * or the {@code fractions} are not provided in strictly increasing order 0N/A * Constructs a {@code LinearGradientPaint}. 0N/A * @param start the gradient axis start {@code Point2D} in user space 0N/A * @param end the gradient axis end {@code Point2D} in user space 0N/A * @param fractions numbers ranging from 0.0 to 1.0 specifying the 0N/A * distribution of colors along the gradient 0N/A * @param colors array of colors corresponding to each fractional value 0N/A * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT}, 0N/A * @param colorSpace which color space to use for interpolation, 0N/A * either {@code SRGB} or {@code LINEAR_RGB} 0N/A * @param gradientTransform transform to apply to the gradient 0N/A * @throws NullPointerException 0N/A * if one of the points is null, 0N/A * or {@code fractions} array is null, 0N/A * or {@code colors} array is null, 0N/A * or {@code cycleMethod} is null, 0N/A * or {@code colorSpace} is null, 0N/A * or {@code gradientTransform} is null 0N/A * @throws IllegalArgumentException 0N/A * if start and end points are the same points, 0N/A * or {@code fractions.length != colors.length}, 0N/A * or {@code colors} is less than 2 in size, 0N/A * or a {@code fractions} value is less than 0.0 or greater than 1.0, 0N/A * or the {@code fractions} are not provided in strictly increasing order 0N/A // check input parameters 0N/A // copy the points... 213N/A * Creates and returns a {@link PaintContext} used to 213N/A * generate a linear color gradient pattern. 213N/A * See the {@link Paint#createContext specification} of the 213N/A * method in the {@link Paint} interface for information 213N/A * on null parameter handling. 213N/A * @param cm the preferred {@link ColorModel} which represents the most convenient 213N/A * format for the caller to receive the pixel data, or {@code null} 213N/A * if there is no preference. 213N/A * @param deviceBounds the device space bounding box 213N/A * of the graphics primitive being rendered. 213N/A * @param userBounds the user space bounding box 213N/A * of the graphics primitive being rendered. 213N/A * @param transform the {@link AffineTransform} from user 213N/A * space into device space. 213N/A * @param hints the set of hints that the context object can use to 213N/A * choose between rendering alternatives. 213N/A * @return the {@code PaintContext} for 213N/A * generating color patterns. 0N/A // avoid modifying the user's transform... 0N/A // incorporate the gradient transform 0N/A // faster to use the basic GradientPaintContext for this 0N/A * Returns a copy of the start point of the gradient axis. 0N/A * @return a {@code Point2D} object that is a copy of the point 0N/A * that anchors the first color of this {@code LinearGradientPaint} 0N/A * Returns a copy of the end point of the gradient axis. 0N/A * @return a {@code Point2D} object that is a copy of the point 0N/A * that anchors the last color of this {@code LinearGradientPaint}