Class GradientPaint
java.lang.Object
java.awt.GradientPaint
- All Implemented Interfaces:
Paint, Transparency
public class GradientPaint extends Object implements Paint
The
GradientPaint class provides a way to fill a Shape with a linear color gradient pattern. If Point P1 with Color C1 and Point P2 with Color C2 are specified in user space, the Color on the P1, P2 connecting line is proportionally changed from C1 to C2. Any point P not on the extended P1, P2 connecting line has the color of the point P' that is the perpendicular projection of P on the extended P1, P2 connecting line. Points on the extended line outside of the P1, P2 segment can be colored in one of two ways. - If the gradient is cyclic then the points on the extended P1, P2 connecting line cycle back and forth between the colors C1 and C2.
- If the gradient is acyclic then points on the P1 side of the segment have the constant
ColorC1 while points on the P2 side have the constantColorC2.
- See Also:
Field Summary
Fields declared in interface Transparency
BITMASK, OPAQUE, TRANSLUCENT | Modifier and Type | Field | Description |
|---|---|---|
static final int |
BITMASK |
Represents image data that is guaranteed to be either completely opaque, with an alpha value of 1.0, or completely transparent, with an alpha value of 0.0. |
static final int |
OPAQUE |
Represents image data that is guaranteed to be completely opaque, meaning that all pixels have an alpha value of 1.0. |
static final int |
TRANSLUCENT |
Represents image data that contains or might contain arbitrary alpha values between and including 0.0 and 1.0. |
Constructor Summary
| Constructor | Description |
|---|---|
GradientPaint |
Constructs a simple acyclic GradientPaint object. |
GradientPaint |
Constructs either a cyclic or acyclic GradientPaint object depending on the boolean parameter. |
GradientPaint |
Constructs a simple acyclic GradientPaint object. |
GradientPaint |
Constructs either a cyclic or acyclic GradientPaint object depending on the boolean parameter. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
PaintContext |
createContext |
Creates and returns a PaintContext used to generate a linear color gradient pattern. |
Color |
getColor1() |
Returns the color C1 anchored by the point P1. |
Color |
getColor2() |
Returns the color C2 anchored by the point P2. |
Point2D |
getPoint1() |
Returns a copy of the point P1 that anchors the first color. |
Point2D |
getPoint2() |
Returns a copy of the point P2 which anchors the second color. |
int |
getTransparency() |
Returns the transparency mode for this GradientPaint. |
boolean |
isCyclic() |
Returns true if the gradient cycles repeatedly between the two colors C1 and C2. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Constructor Details
GradientPaint
public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2)
Constructs a simple acyclic
GradientPaint object.- Parameters:
-
x1- x coordinate of the first specifiedPointin user space -
y1- y coordinate of the first specifiedPointin user space -
color1-Colorat the first specifiedPoint -
x2- x coordinate of the second specifiedPointin user space -
y2- y coordinate of the second specifiedPointin user space -
color2-Colorat the second specifiedPoint - Throws:
-
NullPointerException- if either one of colors is null
GradientPaint
public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2)
Constructs a simple acyclic
GradientPaint object.- Parameters:
-
pt1- the first specifiedPointin user space -
color1-Colorat the first specifiedPoint -
pt2- the second specifiedPointin user space -
color2-Colorat the second specifiedPoint - Throws:
-
NullPointerException- if either one of colors or points is null
GradientPaint
public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)
Constructs either a cyclic or acyclic
GradientPaint object depending on the boolean parameter.- Parameters:
-
x1- x coordinate of the first specifiedPointin user space -
y1- y coordinate of the first specifiedPointin user space -
color1-Colorat the first specifiedPoint -
x2- x coordinate of the second specifiedPointin user space -
y2- y coordinate of the second specifiedPointin user space -
color2-Colorat the second specifiedPoint -
cyclic-trueif the gradient pattern should cycle repeatedly between the two colors;falseotherwise
GradientPaint
@ConstructorProperties({"point1","color1","point2","color2","cyclic"}) public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic) Constructs either a cyclic or acyclic
GradientPaint object depending on the boolean parameter.- Parameters:
-
pt1- the first specifiedPointin user space -
color1-Colorat the first specifiedPoint -
pt2- the second specifiedPointin user space -
color2-Colorat the second specifiedPoint -
cyclic-trueif the gradient pattern should cycle repeatedly between the two colors;falseotherwise - Throws:
-
NullPointerException- if either one of colors or points is null
Method Details
getPoint1
public Point2D getPoint1()
Returns a copy of the point P1 that anchors the first color.
- Returns:
- a
Point2Dobject that is a copy of the point that anchors the first color of thisGradientPaint.
getColor1
public Color getColor1()
Returns the color C1 anchored by the point P1.
- Returns:
- a
Colorobject that is the color anchored by P1.
getPoint2
public Point2D getPoint2()
Returns a copy of the point P2 which anchors the second color.
- Returns:
- a
Point2Dobject that is a copy of the point that anchors the second color of thisGradientPaint.
getColor2
public Color getColor2()
Returns the color C2 anchored by the point P2.
- Returns:
- a
Colorobject that is the color anchored by P2.
isCyclic
public boolean isCyclic()
Returns
true if the gradient cycles repeatedly between the two colors C1 and C2.- Returns:
-
trueif the gradient cycles repeatedly between the two colors;falseotherwise.
createContext
public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints)
Creates and returns a
PaintContext used to generate a linear color gradient pattern. See the specification of the method in the Paint interface for information on null parameter handling.- Specified by:
-
createContextin interfacePaint - Parameters:
-
cm- the preferredColorModelwhich represents the most convenient format for the caller to receive the pixel data, ornullif there is no preference. -
deviceBounds- the device space bounding box of the graphics primitive being rendered. -
userBounds- the user space bounding box of the graphics primitive being rendered. -
xform- theAffineTransformfrom user space into device space. -
hints- the set of hints that the context object can use to choose between rendering alternatives. - Returns:
- the
PaintContextfor generating color patterns. - See Also:
getTransparency
public int getTransparency()
Returns the transparency mode for this
GradientPaint.- Specified by:
-
getTransparencyin interfaceTransparency - Returns:
- an integer value representing this
GradientPaintobject's transparency mode. - See Also:
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.