LinearGradient class is a part of JavaFX. LinearGradient class fills a shape with a linear color gradient pattern. A user may specify more than one LinearGradient pattern and the system will provide interpolation between the colors.
Constructors of the class:
- LinearGradient(double sX, double sY, double eX, double eY, boolean prop, CycleMethod c, List s): Creates a new Lineargradient object.
- LinearGradient(double sX, double sY, double eX, double eY, boolean prop, CycleMethod c, Stop… s): Creates a new LinearGradient object.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| equals(Object o) | Returns whether the LinearGradient objects are equal or not. |
| getCycleMethod() | Returns the cycle method of the linear gradient object. |
| getEndX() | Returns the x coordinate of end point of the linear gradient. |
| getEndY() | Returns the y coordinate of end point of the linear gradient. |
| getStartX() | Returns the x coordinate of start point of the linear gradient. |
| getStartY() | Returns the y coordinate of start point of the linear gradient. |
| getStops() | Returns the stops of linear gradient. |
| isOpaque() | Returns whether the linear Gradient is opaque or not. |
| isProportional() | Returns whether the linear Gradient is proportional or not. |
| valueOf(String v) | Creates a linear gradient value from a string representation. |
Below programs illustrate the use of LinearGradient Class:
- Java program to create a LinearGradient object and add stops to it and apply it to the circle: In this program we will create an array of Stop objects with their offset values ranging from 0 to 1. Create a LinearGradient object with specified stops. Now create a circle with specified x, y positions, and radius and add the linear gradient to it. Then create a VBox and set the alignment of it. Add the circle to the vbox and add the vbox to the scene and add the scene to the stage and call the show() function to display the results.
// Java program to create a LinearGradientÂ// object and add stops to it and apply it// to the circleimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*; ÂÂpublicclassLinear_Gradient_1extendsApplication { ÂÂ// launch the applicationpublicvoidstart(Stage stage){   Âtry{       Â// set title for the stage       Âstage.setTitle("Linear Gradient");       Â// create stops       ÂStop[] stop = {newStop(0, Color.RED),                      ÂnewStop(0.5, Color.GREEN),                      ÂnewStop(1, Color.BLUE)};       Â// create a Linear gradient object       ÂLinearGradient linear_gradient =newLinearGradient(0,0,                         Â1,0,true, CycleMethod.NO_CYCLE, stop);       Â// create a circle       ÂCircle circle =newCircle(100,100,70);       Â// set fill       Âcircle.setFill(linear_gradient);       Â// create VBox       ÂVBox vbox =newVBox(circle);       Â// ste Alignment       Âvbox.setAlignment(Pos.CENTER);       Â// create a scene       ÂScene scene =newScene(vbox,400,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Âcatch(Exception e) {       ÂSystem.out.println(e.getMessage());   Â}}ÂÂ// Main Methodpublicstaticvoidmain(String args[]){   Â// launch the application   Âlaunch(args);}}Output:
- Java program to create a LinearGradient object and add stops to it and set the CycleMethod to reflect and set proportional to false and apply it to the circle: In this program we will create an array of Stop objects with their offset values ranging from 0 to 1. Then create a LinearGradient object with specified stops. Set the CycleMethod to reflect and set proportional to false. Create a circle with specified x, y positions, and radius and add the linear gradient to it. Then create a VBox and set the alignment of it. Add the circle to the vbox and add the vbox to the scene and add the scene to the stage and call the show() function to display the results.
// Java program to create a LinearGradient objectÂ// and add stops to it and set the CycleMethod to// reflect and set proportional to false andÂ// apply it to the circleimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*;ÂÂpublicclassLinear_Gradient_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("Linear Gradient");           Â// create stops           ÂStop[] stop = {newStop(0, Color.RED),newStop(0.5,                        ÂColor.GREEN),newStop(1, Color.BLUE)};           Â// create a Linear gradient object           ÂLinearGradient linear_gradient =newLinearGradient(0,0,                           Â35,0,false, CycleMethod.REFLECT, stop);           Â// create a circle           ÂCircle circle =newCircle(100,100,70);           Â// set fill           Âcircle.setFill(linear_gradient);           Â// create VBox           ÂVBox vbox =newVBox(circle);           Â// ste Alignment           Âvbox.setAlignment(Pos.CENTER);           Â// create a scene           ÂScene scene =newScene(vbox,400,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/LinearGradient.html

