CycleMethod class is a part of JavaFX. CycleMethod defines the method to use when painting outside the gradient bounds. It contains 3 Enum Constants as follows:
- NO_CYCLE: Used to define the cycle method which uses terminal colors to fill the remaining area.
- REFLECT: Used to define the cycle method which reflects the gradient colors, start to end then end to start.
- REPEAT: Used to define the cycle method which repeats the gradient colors to fill the remaining area.
Commonly Used Method:
| Method | Explanation |
|---|---|
| valueOf(String name) | Returns the CycleMethod of the specified name. |
| values() | Returns an array which contains the values of Cyclemethod type. |
Below programs illustrate the use of the CycleMethod class:
- Java program to create a LinearGradient object, add stops to it, set the CycleMethod to repeat, set proportional to false and apply it to the rectangle:
- 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 repeat and set proportional to false. then create a circle with specified x, y positions, and radius and add the linear gradient to it.
- After that 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,// add stops to it, set the CycleMethod to repeat,// set proportional to false and apply it to the// rectangleimportjavafx.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.*;ÂÂpublicclassCycleMethod_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("CycleMethod");           Â// create stops           ÂStop[] stop = {newStop(0, Color.RED),                        ÂnewStop(1, Color.BLUE)};           Â// create a Linear gradient object           ÂLinearGradient linear_gradient =newLinearGradient(0,0,                            Â35,0,false, CycleMethod.REPEAT, stop);           Â// create a rectangle           ÂRectangle rectangle =newRectangle(100,100,100,70);           Â// set fill           Ârectangle.setFill(linear_gradient);           Â// create VBox           ÂVBox vbox =newVBox(rectangle);           Â// 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:
- Java program to create a LinearGradient object, add stops to it, set the CycleMethod to reflect, 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. 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.
- Call the show() function to display the results.
// Java program to create a LinearGradient object,// add stops to it, set the CycleMethod to reflect,// 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.*;ÂÂpublicclassCycleMethod_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("CycleMethod");           Â// create stops           ÂStop[] stop = {newStop(0, Color.RED),                        ÂnewStop(1, Color.BLUE)};           Â// create a Linear gradient object           ÂLinearGradient linear_gradient =newLinearGradient(0,0,                           Â35,0,false, CycleMethod.REFLECT, stop);           Â// create a rectangle           ÂRectangle rectangle =newRectangle(100,100,100,70);           Â// set fill           Ârectangle.setFill(linear_gradient);           Â// create VBox           ÂVBox vbox =newVBox(rectangle);           Â// 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:
- Java program to create LinearGradient object, add stops to it, set the CycleMethod to no cycle, set proportional to false and apply it to the rectangle:
- 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.
- Set the CycleMethod to no cycle and set proportional to false. Then Create a circle with specified x, y positions, and radius, and add the linear gradient to it. Create a VBox and set the alignment of it.
- Now 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 LinearGradient object,// add stops to it, set the CycleMethod to noÂ// cycle, set proportional to false and applyÂ// it to the rectangleimportjavafx.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.*;ÂÂpublicclassCycleMethod_3extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("CycleMethod");           Â// create stops           ÂStop[] stop = {newStop(0, Color.RED),                        ÂnewStop(1, Color.BLUE)};           Â// create a Linear gradient object           ÂLinearGradient linear_gradient =newLinearGradient(0,0,                          Â35,0,false, CycleMethod.NO_CYCLE, stop);           Â// create a rectangle           ÂRectangle rectangle =newRectangle(100,100,100,70);           Â// set fill           Ârectangle.setFill(linear_gradient);           Â// create VBox           ÂVBox vbox =newVBox(rectangle);           Â// 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/javafx/2/api/javafx/scene/paint/CycleMethod.html

