×

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the setFill() method. The setFill() method has a gradient object passed in it. CycleMethod argument allows to set whether cycles are allowed or not.

Filling the Gradient color by specifying No cycle in JavaFX:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.*;


public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Stop[] newstop = {new Stop(0.5, Color.RED), 
                 new Stop(0.5, Color.GREEN), 
                 new Stop(1, Color.BLUE)};


          LinearGradient newgradient = new LinearGradient(0, 0,
                    1, 0, true, CycleMethod.NO_CYCLE, newstop);


     Rectangle newrect = new Rectangle(); 
newrect.setX(80); 
newrect.setY(80); 
newrect.setWidth(200); 
newrect.setHeight(200);
newrect.setFill(newgradient);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Setting Linear Gradient with no cycle ");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill gradient color in it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.paint.*, javafx.scene.shape.*, javafx.scene.shape.Rectangle.

Then we have created one class named ColorUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Gradient Colored Rectangle with no cycles, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the gradient color of the Rectangle. Linear Gradient constructor is called with values and Cycle method value specified in it. This object is then passed in the setFill() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Setting Linear Gradient with no cycle”. Also, it displays a rectangle with defined x, y coordinates, and width, height with gradient color filled in it, and no cycles for the specified gradient.

JavaFX Gradient Color

Filling the Gradient color by specifying cycle in JavaFX:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.*;


public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Stop[] newstop = {new Stop(0, Color.RED), new Stop(0.5, 
                Color.GREEN), new Stop(1, Color.BLUE)};


   LinearGradient newgradient = new LinearGradient(0, 0, 
                   35, 0, false, CycleMethod.REFLECT, newstop);




     Rectangle newrect = new Rectangle(); 
newrect.setX(80); 
newrect.setY(80); 
newrect.setWidth(200); 
newrect.setHeight(200);
newrect.setFill(newgradient);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Setting Linear Gradient with cycle Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill gradient color in it with cycles in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.paint.*, javafx.scene.shape.*, javafx.scene.shape.Rectangle.

Then we have created one class named ColorUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Gradient Colored Rectangle with cycles, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the gradient color of the Rectangle. Linear Gradient constructor is called with values and Cycle method as REFLECT in it. This object is then passed in the setFill() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Setting Linear Gradient with cycle Example”. Also, it displays a rectangle with defined x, y coordinates, and width, height with gradient color filled in it, and reflected cycles for the specified gradient.

JavaFX Gradient Color

Filling the Radial Gradient color by specifying No cycle in JavaFX:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.*;


public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    RadialGradient newgradient = new RadialGradient(0, .1,  100, 100, 200, false,  CycleMethod.NO_CYCLE,  
                new Stop(0, Color.RED),  
                new Stop(1, Color.GREEN));
              
     Rectangle newrect = new Rectangle(); 
newrect.setX(80); 
newrect.setY(80); 
newrect.setWidth(200); 
newrect.setHeight(200);
newrect.setFill(newgradient);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Setting Radial Gradient with no cycle Example");  
           primaryStage.show();  
        }


    public static void main(String[] args) 
   {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill radial gradient color in it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.paint.*, javafx.scene.shape.*, javafx.scene.shape.Rectangle.

Then we have created one class named ColorUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Radial Gradient Colored Rectangle with no cycles, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the gradient color of the Rectangle. Radial Gradient constructor is called with values and Cycle method value specified in it. This object is then passed in the setFill() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX 2D shapes - Setting Radial Gradient with no cycle Example”. Also, it displays a rectangle with defined x, y coordinates, and width, height with radial gradient color filled in it, and no cycles for the specified gradient.

JavaFX Gradient Color

Filling the Semitransparent Gradient color in JavaFX:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.*;


public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    LinearGradient newgradient = new LinearGradient(0,0,0,1,true,
                CycleMethod.NO_CYCLE, 
                new Stop(0.1f, Color.rgb(25, 80, 240, .4)),
                new Stop(1.0f, Color.rgb(100, 100, 100, .5)));


     Rectangle newrect = new Rectangle(); 
newrect.setX(80); 
newrect.setY(80); 
newrect.setWidth(200); 
newrect.setHeight(200);
newrect.setFill(newgradient);  


         Group box = new Group();  
         box.getChildren().addAll(newrect);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 2D shapes - Setting semitransperant Gradient Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill semitransparent gradient color in it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.paint.*, javafx.scene.shape.*, javafx.scene.shape.Rectangle.

Then we have created one class named ColorUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a semitransparent Gradient Colored Rectangle with no cycles, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of an empty constructor and X, Y, Width, and Height is set using setters and the setFill() method is used to set the gradient color of the Rectangle. Linear Gradient constructor is called with values specified in it. This object is then passed in the setFill() method.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Setting semitransperant Gradient Example”. Also, it displays a rectangle with defined x, y coordinates, and width, height with semitransperant gradient color filled in it.

JavaFX Gradient Color

Related Topics

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.