Path: blob/master/src/java.desktop/share/classes/java/awt/GradientPaint.java
41152 views
/*1* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.geom.Point2D;28import java.awt.geom.Rectangle2D;29import java.awt.geom.AffineTransform;30import java.awt.image.ColorModel;31import java.beans.ConstructorProperties;3233/**34* The {@code GradientPaint} class provides a way to fill35* a {@link Shape} with a linear color gradient pattern.36* If {@link Point} P1 with {@link Color} C1 and {@code Point} P2 with37* {@code Color} C2 are specified in user space, the38* {@code Color} on the P1, P2 connecting line is proportionally39* changed from C1 to C2. Any point P not on the extended P1, P240* connecting line has the color of the point P' that is the perpendicular41* projection of P on the extended P1, P2 connecting line.42* Points on the extended line outside of the P1, P2 segment can be colored43* in one of two ways.44* <ul>45* <li>46* If the gradient is cyclic then the points on the extended P1, P247* connecting line cycle back and forth between the colors C1 and C2.48* <li>49* If the gradient is acyclic then points on the P1 side of the segment50* have the constant {@code Color} C1 while points on the P2 side51* have the constant {@code Color} C2.52* </ul>53*54* @see Paint55* @see Graphics2D#setPaint56* @version 10 Feb 199757*/5859public class GradientPaint implements Paint {60Point2D.Float p1;61Point2D.Float p2;62Color color1;63Color color2;64boolean cyclic;6566/**67* Constructs a simple acyclic {@code GradientPaint} object.68* @param x1 x coordinate of the first specified69* {@code Point} in user space70* @param y1 y coordinate of the first specified71* {@code Point} in user space72* @param color1 {@code Color} at the first specified73* {@code Point}74* @param x2 x coordinate of the second specified75* {@code Point} in user space76* @param y2 y coordinate of the second specified77* {@code Point} in user space78* @param color2 {@code Color} at the second specified79* {@code Point}80* @throws NullPointerException if either one of colors is null81*/82public GradientPaint(float x1,83float y1,84Color color1,85float x2,86float y2,87Color color2) {88if ((color1 == null) || (color2 == null)) {89throw new NullPointerException("Colors cannot be null");90}9192p1 = new Point2D.Float(x1, y1);93p2 = new Point2D.Float(x2, y2);94this.color1 = color1;95this.color2 = color2;96}9798/**99* Constructs a simple acyclic {@code GradientPaint} object.100* @param pt1 the first specified {@code Point} in user space101* @param color1 {@code Color} at the first specified102* {@code Point}103* @param pt2 the second specified {@code Point} in user space104* @param color2 {@code Color} at the second specified105* {@code Point}106* @throws NullPointerException if either one of colors or points107* is null108*/109public GradientPaint(Point2D pt1,110Color color1,111Point2D pt2,112Color color2) {113if ((color1 == null) || (color2 == null) ||114(pt1 == null) || (pt2 == null)) {115throw new NullPointerException("Colors and points should be non-null");116}117118p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());119p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());120this.color1 = color1;121this.color2 = color2;122}123124/**125* Constructs either a cyclic or acyclic {@code GradientPaint}126* object depending on the {@code boolean} parameter.127* @param x1 x coordinate of the first specified128* {@code Point} in user space129* @param y1 y coordinate of the first specified130* {@code Point} in user space131* @param color1 {@code Color} at the first specified132* {@code Point}133* @param x2 x coordinate of the second specified134* {@code Point} in user space135* @param y2 y coordinate of the second specified136* {@code Point} in user space137* @param color2 {@code Color} at the second specified138* {@code Point}139* @param cyclic {@code true} if the gradient pattern should cycle140* repeatedly between the two colors; {@code false} otherwise141*/142public GradientPaint(float x1,143float y1,144Color color1,145float x2,146float y2,147Color color2,148boolean cyclic) {149this (x1, y1, color1, x2, y2, color2);150this.cyclic = cyclic;151}152153/**154* Constructs either a cyclic or acyclic {@code GradientPaint}155* object depending on the {@code boolean} parameter.156* @param pt1 the first specified {@code Point}157* in user space158* @param color1 {@code Color} at the first specified159* {@code Point}160* @param pt2 the second specified {@code Point}161* in user space162* @param color2 {@code Color} at the second specified163* {@code Point}164* @param cyclic {@code true} if the gradient pattern should cycle165* repeatedly between the two colors; {@code false} otherwise166* @throws NullPointerException if either one of colors or points167* is null168*/169@ConstructorProperties({ "point1", "color1", "point2", "color2", "cyclic" })170public GradientPaint(Point2D pt1,171Color color1,172Point2D pt2,173Color color2,174boolean cyclic) {175this (pt1, color1, pt2, color2);176this.cyclic = cyclic;177}178179/**180* Returns a copy of the point P1 that anchors the first color.181* @return a {@link Point2D} object that is a copy of the point182* that anchors the first color of this183* {@code GradientPaint}.184*/185public Point2D getPoint1() {186return new Point2D.Float(p1.x, p1.y);187}188189/**190* Returns the color C1 anchored by the point P1.191* @return a {@code Color} object that is the color192* anchored by P1.193*/194public Color getColor1() {195return color1;196}197198/**199* Returns a copy of the point P2 which anchors the second color.200* @return a {@link Point2D} object that is a copy of the point201* that anchors the second color of this202* {@code GradientPaint}.203*/204public Point2D getPoint2() {205return new Point2D.Float(p2.x, p2.y);206}207208/**209* Returns the color C2 anchored by the point P2.210* @return a {@code Color} object that is the color211* anchored by P2.212*/213public Color getColor2() {214return color2;215}216217/**218* Returns {@code true} if the gradient cycles repeatedly219* between the two colors C1 and C2.220* @return {@code true} if the gradient cycles repeatedly221* between the two colors; {@code false} otherwise.222*/223public boolean isCyclic() {224return cyclic;225}226227/**228* Creates and returns a {@link PaintContext} used to229* generate a linear color gradient pattern.230* See the {@link Paint#createContext specification} of the231* method in the {@link Paint} interface for information232* on null parameter handling.233*234* @param cm the preferred {@link ColorModel} which represents the most convenient235* format for the caller to receive the pixel data, or {@code null}236* if there is no preference.237* @param deviceBounds the device space bounding box238* of the graphics primitive being rendered.239* @param userBounds the user space bounding box240* of the graphics primitive being rendered.241* @param xform the {@link AffineTransform} from user242* space into device space.243* @param hints the set of hints that the context object can use to244* choose between rendering alternatives.245* @return the {@code PaintContext} for246* generating color patterns.247* @see Paint248* @see PaintContext249* @see ColorModel250* @see Rectangle251* @see Rectangle2D252* @see AffineTransform253* @see RenderingHints254*/255public PaintContext createContext(ColorModel cm,256Rectangle deviceBounds,257Rectangle2D userBounds,258AffineTransform xform,259RenderingHints hints) {260261return new GradientPaintContext(cm, p1, p2, xform,262color1, color2, cyclic);263}264265/**266* Returns the transparency mode for this {@code GradientPaint}.267* @return an integer value representing this {@code GradientPaint}268* object's transparency mode.269* @see Transparency270*/271public int getTransparency() {272int a1 = color1.getAlpha();273int a2 = color2.getAlpha();274return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);275}276277}278279280