JavaFX | CycleMethod Class

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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("CycleMethod");// create stopsStop[] stop = {newStop(0, Color.RED),newStop(1, Color.BLUE)};// create a Linear gradient objectLinearGradient linear_gradient =newLinearGradient(0,0,35,0,false, CycleMethod.REPEAT, stop);// create a rectangleRectangle rectangle =newRectangle(100,100,100,70);// set fillrectangle.setFill(linear_gradient);// create VBoxVBox vbox =newVBox(rectangle);// ste Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("CycleMethod");// create stopsStop[] stop = {newStop(0, Color.RED),newStop(1, Color.BLUE)};// create a Linear gradient objectLinearGradient linear_gradient =newLinearGradient(0,0,35,0,false, CycleMethod.REFLECT, stop);// create a rectangleRectangle rectangle =newRectangle(100,100,100,70);// set fillrectangle.setFill(linear_gradient);// create VBoxVBox vbox =newVBox(rectangle);// ste Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("CycleMethod");// create stopsStop[] stop = {newStop(0, Color.RED),newStop(1, Color.BLUE)};// create a Linear gradient objectLinearGradient linear_gradient =newLinearGradient(0,0,35,0,false, CycleMethod.NO_CYCLE, stop);// create a rectangleRectangle rectangle =newRectangle(100,100,100,70);// set fillrectangle.setFill(linear_gradient);// create VBoxVBox vbox =newVBox(rectangle);// ste Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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




