JavaFX Gradient Color

In Computer Graphics, Gradient Colors (sometimes called Color Progression ) are used to specify the position dependent colors to fill a particular region. The value of the gradient color varies with the position. Gradient colors produces the smooth color transitions on the region by varying the color value continuously with the position.

JavaFX enables us to implement two types of Gradient color transitions:

  1. Linear Gradient
  2. Radial Gradient

Linear Gradient

To apply linear gradient patterns to the shapes, we need to instantiate the LinearGradient class. This class contains several instance methods described below in the table.

Instance Methods

TypeMethodDescription
Booleanequals(Object o)Compares two objects
CycleMethodgetCycleMethod()Defines which cycle method has been applied to LinearGradient.
DoublegetEndX()X coordinate of gradient axis end point
DoublegetEndY()Y coordinate of gradient axis end point
DoublegetStartX()X coordinate of gradient axis start point
DoublegetStartY()Y coordinate of gradient axis start point
List<Stop>getStops()Defines the way of distributions of colors along the gradient
InthashCode()Returns hash code for the linear gradient object
BooleanisOpaque()Check whether the paint is completely opaque or not.
BooleanisProprtional()Checks whether the start and end locations are proportional or not.
StringtoString()Convert Gradient object to string.

Constructors

The Constructor of this class accepts five parameters:

new LinearGradient(startX, startY, endX, endY, Proportional, CycleMethod, stops)

(startX,startY): represents x and y coordinates of the starting point of the gradient color.

(endX,endY): represents x and y coordinates of the ending point of the gradient color.

Proportional: This is a boolean type property. If this is true then the starting and ending point of the gradient color will become proportional.

CycleMethod: This defines the cycle method applied to the gradient.

Stops: this defines the color distribution along the gradient.

Example:


JavaFX Gradient Color Output1

Radial Gradient

To apply Radial gradient to the shapes , we need to instantiate javafx.scene.paint.RadialGradient class. This class contains various instance methods described in the table below.

TypeMethodDescription
Booleanequals(Object o)Compares two objects
DoublegetCenterX()X coordinate of the circle which is defining the gradient
DoublegetCenterY()Y coordinate of the circle which is defining the gradient
CycleMethodgetCycleMethod()Defines which cycle method has been applied to LinearGradient.
DoublegetFocusAngle()Angle in degrees between the centre of the gradient and the focus of the position where first color is mapped
DoublegetFocusDistance()Distance between the Centre of the gradient and the focus point of the first color.
DoublegetRadiusRadius of the gradient
List<Stop>getStops()Defines the way of distributions of colors along the gradient
InthashCode()Returns hash code for the linear gradient object
BooleanisOpaque()Check whether the paint is completely opaque or not.
BooleanisProprtional()Checks whether the start and end locations are proportional or not.
StringtoString()Convert Gradient object to string.

Constructor

The constructor of the class accepts a few parameters. The Syntax of the constructor is given below.

Example


JavaFX Gradient Color Output2 Output
Next TopicJavaFX Text




Latest Courses