Top Description Fields Constructors Methods
javafx.scene.paint

public final Class LinearGradient

extends Paint
Class Inheritance
Imports
java.util.List, .Objects, com.sun.javafx.scene.paint.GradientUtils, com.sun.javafx.tk.Toolkit, com.sun.javafx.util.InterpolationUtils, javafx.beans.NamedArg

The LinearGradient class fills a shape with a linear color gradient pattern. The user may specify two or more gradient colors, and this Paint will provide an interpolation between each color.

The application provides an array of Stops specifying how to distribute the colors along the gradient. The Stop#offset variable must be the range 0.0 to 1.0 and act like keyframes along the gradient. The offsets mark where the gradient should be exactly a particular color.

If the proportional variable is set to true then the start and end points of the gradient should be specified relative to the unit square (0.0->1.0) and will be stretched across the shape. If the proportional variable is set to false, then the start and end points should be specified in the local coordinate system of the shape and the gradient will not be stretched at all.

The two filled rectangles in the example below will render the same. The one on the left uses proportional coordinates to specify the end points of the gradient. The one on the right uses absolute coordinates. Both of them fill the specified rectangle with a horizontal gradient that varies from black to red

// object bounding box relative (proportional = true)
Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED)};
LinearGradient lg1 = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
Rectangle r1 = new Rectangle(0, 0, 100, 100);
r1.setFill(lg1);

// user space relative (proportional: = false)
LinearGradient lg2 = new LinearGradient(125, 0, 225, 0, false, CycleMethod.NO_CYCLE, stops);
Rectangle r2 = new Rectangle(125, 0, 100, 100);
r2.setFill(lg2);
Since
JavaFX 2.0

Field Summary

Modifier and TypeField and Description
private CycleMethod
private double
private double
private int
hash

The cached hash code, used to improve performance in situations where we cache gradients, such as in the CSS routines.

private final boolean
private Object
platformPaint

A cached reference to the platform paint, no point recomputing twice

private boolean
private double
private double
private List<Stop>

Constructor Summary

AccessConstructor and Description
public
LinearGradient(double
the X coordinate of the gradient axis start point
startX
,
double
the Y coordinate of the gradient axis start point
startY
,
double
the X coordinate of the gradient axis end point
endX
,
double
the Y coordinate of the gradient axis end point
endY
,
boolean
whether the coordinates are proportional to the shape which this gradient fills
proportional
,
CycleMethod
cycle method applied to the gradient
cycleMethod
,
Stop...
the gradient's color specification
stops
)

Creates a new instance of LinearGradient.

public
LinearGradient(double
the X coordinate of the gradient axis start point
startX
,
double
the Y coordinate of the gradient axis start point
startY
,
double
the X coordinate of the gradient axis end point
endX
,
double
the Y coordinate of the gradient axis end point
endY
,
boolean
whether the coordinates are proportional to the shape which this gradient fills
proportional
,
CycleMethod
cycle method applied to the gradient
cycleMethod
,
List<Stop>
the gradient's color specification
stops
)

Creates a new instance of LinearGradient.

private
LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops, int ignored)

Private constructor accepting a stop list that is already normalized.

Method Summary

Modifier and TypeMethod and Description
pack-priv Object
private boolean

Returns:

Whether this gradient is opaque
determineOpacity
()

Iterate over all the stops.

public boolean

Returns:

true if this object is equal to the obj argument; false otherwise.
equals
(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public final CycleMethod

Returns:

the cycle method applied to this linear gradient
getCycleMethod
()

Defines which of the following cycle method is applied to the LinearGradient: CycleMethod.NO_CYCLE, CycleMethod.REFLECT, or CycleMethod.REPEAT.

public final double

Returns:

the X coordinate of the gradient axis end point
getEndX
()

Defines the X coordinate of the gradient axis end point.

public final double

Returns:

the Y coordinate of the gradient axis end point
getEndY
()

Defines the Y coordinate of the gradient axis end point.

public final double

Returns:

the X coordinate of the gradient axis start point
getStartX
()

Defines the X coordinate of the gradient axis start point.

public final double

Returns:

the Y coordinate of the gradient axis start point
getStartY
()

Defines the Y coordinate of the gradient axis start point.

public final List<Stop>

Returns:

the list of stop values
getStops
()

A sequence of 2 or more Stop values specifying how to distribute the colors along the gradient.

public int

Returns:

a hash code for this LinearGradient object.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code for this LinearGradient object.

public LinearGradient

Returns:

the intermediate value
interpolate
(LinearGradient
the target value
endValue
,
double
the interpolation factor
t
)

Implements javafx.animation.Interpolatable.interpolate.

Returns an intermediate value between the value of this LinearGradient and the specified endValue using the linear interpolation factor t, ranging from 0 (inclusive) to 1 (inclusive).

public Paint
interpolate(Paint
the target value
endValue
,
double
the interpolation factor
t
)

Implements javafx.animation.Interpolatable.interpolate.

Returns an intermediate value between the value of this Interpolatable and the specified endValue using the linear interpolation factor t, ranging from 0 (inclusive) to 1 (inclusive).

public final boolean
isOpaque()

Implements abstract javafx.scene.paint.Paint.isOpaque.

Gets whether this Paint is completely opaque.

public final boolean

Returns:

if true start and end locations are proportional, otherwise absolute
isProportional
()

Indicates whether start and end locations are proportional or absolute.

private boolean
isSame(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops)

public String

Returns:

a string representation of this LinearGradient object.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this LinearGradient object.

public static LinearGradient

Returns:

a LinearGradient object holding the value represented by the string argument.
valueOf
(String
the string to convert
value
)

Hides javafx.scene.paint.Paint.valueOf.

Creates a linear gradient value from a string representation.

Inherited from javafx.scene.paint.Paint:
acc_addListeneracc_isMutableacc_removeListener

Field Detail

cycleMethodback to summary
private CycleMethod cycleMethod
endXback to summary
private double endX
endYback to summary
private double endY
hashback to summary
private int hash

The cached hash code, used to improve performance in situations where we cache gradients, such as in the CSS routines.

opaqueback to summary
private final boolean opaque
platformPaintback to summary
private Object platformPaint

A cached reference to the platform paint, no point recomputing twice

proportionalback to summary
private boolean proportional
startXback to summary
private double startX
startYback to summary
private double startY
stopsback to summary
private List<Stop> stops

Constructor Detail

LinearGradientback to summary
public LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, Stop... stops)

Creates a new instance of LinearGradient.

Parameters
startX:double

the X coordinate of the gradient axis start point

startY:double

the Y coordinate of the gradient axis start point

endX:double

the X coordinate of the gradient axis end point

endY:double

the Y coordinate of the gradient axis end point

proportional:boolean

whether the coordinates are proportional to the shape which this gradient fills

cycleMethod:CycleMethod

cycle method applied to the gradient

stops:Stop[]

the gradient's color specification

LinearGradientback to summary
public LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops)

Creates a new instance of LinearGradient.

Parameters
startX:double

the X coordinate of the gradient axis start point

startY:double

the Y coordinate of the gradient axis start point

endX:double

the X coordinate of the gradient axis end point

endY:double

the Y coordinate of the gradient axis end point

proportional:boolean

whether the coordinates are proportional to the shape which this gradient fills

cycleMethod:CycleMethod

cycle method applied to the gradient

stops:List<Stop>

the gradient's color specification

LinearGradientback to summary
private LinearGradient(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops, int ignored)

Private constructor accepting a stop list that is already normalized. This constructor is only called from the interpolate method.

Method Detail

acc_getPlatformPaintback to summary
pack-priv Object acc_getPlatformPaint()

Implements abstract javafx.scene.paint.Paint.acc_getPlatformPaint.

Annotations
@Override
determineOpacityback to summary
private boolean determineOpacity()

Iterate over all the stops. If any one of them has a transparent color, then we return false. If there are no stops, we return false. Otherwise, we return true. Note that this is called AFTER Stop.normalize, which ensures that we always have at least 2 stops.

Returns:boolean

Whether this gradient is opaque

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is equal to the obj argument; false otherwise.

Annotations
@Override
getCycleMethodback to summary
public final CycleMethod getCycleMethod()

Defines which of the following cycle method is applied to the LinearGradient: CycleMethod.NO_CYCLE, CycleMethod.REFLECT, or CycleMethod.REPEAT.

Returns:CycleMethod

the cycle method applied to this linear gradient

Default Value
NO_CYCLE
getEndXback to summary
public final double getEndX()

Defines the X coordinate of the gradient axis end point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the shape that the gradient fills.

Returns:double

the X coordinate of the gradient axis end point

Default Value
1.0
getEndYback to summary
public final double getEndY()

Defines the Y coordinate of the gradient axis end point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the shape that the gradient fills.

Returns:double

the Y coordinate of the gradient axis end point

Default Value
1.0
getStartXback to summary
public final double getStartX()

Defines the X coordinate of the gradient axis start point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the shape that the gradient fills.

Returns:double

the X coordinate of the gradient axis start point

Default Value
0.0
getStartYback to summary
public final double getStartY()

Defines the Y coordinate of the gradient axis start point. If proportional is true (the default), this value specifies a point on a unit square that will be scaled to match the size of the shape that the gradient fills.

Returns:double

the Y coordinate of the gradient axis start point

Default Value
0.0
getStopsback to summary
public final List<Stop> getStops()

A sequence of 2 or more Stop values specifying how to distribute the colors along the gradient. These values must be in the range 0.0 to 1.0. They act like key frames along the gradient: they mark where the gradient should be exactly a particular color.

Each stop in the sequence must have an offset that is greater than the previous stop in the sequence.

The list is unmodifiable and will throw UnsupportedOperationException on each modification attempt.

Returns:List<Stop>

the list of stop values

Default Value
empty
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code for this LinearGradient object.

Returns:int

a hash code for this LinearGradient object.

Annotations
@Override
interpolateback to summary
public LinearGradient interpolate(LinearGradient endValue, double t)

Implements javafx.animation.Interpolatable.interpolate.

Returns an intermediate value between the value of this LinearGradient and the specified endValue using the linear interpolation factor t, ranging from 0 (inclusive) to 1 (inclusive).

Parameters
endValue:LinearGradient

the target value

t:double

the interpolation factor

Returns:LinearGradient

the intermediate value

Exceptions
NullPointerException:
if endValue is null
Since
24
interpolateback to summary
public Paint interpolate(Paint endValue, double t)

Implements javafx.animation.Interpolatable.interpolate.

Doc from javafx.animation.Interpolatable.interpolate.

Returns an intermediate value between the value of this Interpolatable and the specified endValue using the linear interpolation factor t, ranging from 0 (inclusive) to 1 (inclusive).

The returned value might not be a new instance; the implementation might also return one of the two existing instances if the intermediate value would be equal to one of the existing values. However, this is an optimization and applications should not assume any particular identity of the returned value.

Parameters
endValue:Paint

the target value

t:double

the interpolation factor

Returns:Paint

the intermediate value

Annotations
@Override
Exceptions
NullPointerException:
if endValue is null
Since
24
isOpaqueback to summary
public final boolean isOpaque()

Implements abstract javafx.scene.paint.Paint.isOpaque.

Doc from javafx.scene.paint.Paint.isOpaque.

Gets whether this Paint is completely opaque. An opaque paint is one that has no alpha component in any of its colors. It may be possible for a Paint to be opaque and yet return false, if it cannot be easily determined whether the paint is actually opaque. For example, the ImagePattern may not be able to cheaply determine its opacity.

Returns:boolean

true if the Paint is opaque, false if it is not opaque or if it cannot be determined.

Annotations
@Override
Since
JavaFX 8.0
isProportionalback to summary
public final boolean isProportional()

Indicates whether start and end locations are proportional or absolute. If this flag is true, the two end points are defined in a coordinate space where coordinates in the range [0..1] are scaled to map onto the bounds of the shape that the gradient fills. If this flag is false, then the coordinates are specified in the local coordinate system of the node.

Returns:boolean

if true start and end locations are proportional, otherwise absolute

Default Value
true
isSameback to summary
private boolean isSame(double startX, double startY, double endX, double endY, boolean proportional, CycleMethod cycleMethod, List<Stop> stops)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this LinearGradient object.

Returns:String

a string representation of this LinearGradient object.

Annotations
@Override
valueOfback to summary
public static LinearGradient valueOf(String value)

Hides javafx.scene.paint.Paint.valueOf.

Creates a linear gradient value from a string representation.

The format of the string representation is based on JavaFX CSS specification for linear gradient which is

linear-gradient( [ [from <point> to <point>| [ to <side-or-corner>], ]? [ [ repeat | reflect ], ]? <color-stop>[, <color-stop>]+)
where
<side-or-corner> = [left | right] || [top | bottom]
<point> = [ [ <length> <length> ] | [ <percentage> | <percentage> ] ]
<color-stop> = [ <color> [ <percentage> | <length>]? ]

Currently length can be only specified in px, the specification of unit can be omitted. Format of color representation is the one used in Color#web(String color). The linear-gradient keyword can be omitted. For additional information about the format of string representation, see the CSS Reference Guide.

Examples:
LinearGradient g
     = LinearGradient.valueOf("linear-gradient(from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%)");
LinearGradient g
     = LinearGradient.valueOf("from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%");
LinearGradient g
     = LinearGradient.valueOf("linear-gradient(from 0px 0px to 200px 0px, #00ff00 0%, 0xff0000 50%, 0x1122ff40 100%)");
LinearGradient g
     = LinearGradient.valueOf("from 0px 0px to 200px 0px, #00ff00 0%, 0xff0000 50%, 0x1122ff40 100%");
Parameters
value:String

the string to convert

Returns:LinearGradient

a LinearGradient object holding the value represented by the string argument.

Exceptions
NullPointerException:
if the value is null
IllegalArgumentException:
if the value cannot be parsed
Since
JavaFX 2.1