×

JavaFX 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 colors in these shapes using the setFill() method. The setFill() method have a color class argument passed in it and it is called using JavaFX Shape Argument.

Filling the color by specifying the color name in JavaFX:

Example:

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


public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(80); 
    newrect.setY(80); 
    newrect.setWidth(200); 
    newrect.setHeight(200);
    newrect.setFill(Color.BROWN);  


         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 color by color name example ");  
           primaryStage.show();  
        }


    public static void main(String[] args) {


        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill 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.Color, 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 Colored Rectangle, 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 color of the Rectangle. The color argument is passed in the setFill() method and the color is set in the JavaFX by specifying the color name.

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 color by color name example”. Also, it displays a rectangle with defined x, y coordinates, and width, height with Brown color filled in it.

JavaFX Color

Filling the color by specifying RGB color values in JavaFX:

RGB stands for red, green, and blue. It is the most commonly used color in the graphics system. Every component has a value ranging from 0 to 255. The (0, 0, 0) indicates black color and (255, 255, 255) denotes white color.

Example:

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




public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(80); 
    newrect.setY(80); 
    newrect.setWidth(200); 
    newrect.setHeight(200);
    int red = 65;
    int green = 100;
    int blue = 150;
    newrect.setFill(Color.rgb(red, green, blue));


         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 color by RGB values ");  
           primaryStage.show();  
        }


    public static void main(String[] args) 
{


        Application.launch(args);
    }

Output:

In order to create the Rectangle shape and fill it in using RGB values 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.Color, 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 Rectangle filled using RGB values, 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 color of the Rectangle. Static method rgb() belonging to Color class is passed in setFill() method with red, green, blue values mentioned in it.

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 color by RGB values ”. Also, it displays a rectangle with defined x, y coordinates, and width, height with specified RGB value color filled in it.

JavaFX Color

Filling the color by specifying the HSB values in JavaFX:

Example:

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


  
public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(80); 
    newrect.setY(80); 
    newrect.setWidth(200); 
    newrect.setHeight(200);
    int Hue = 150;
    int Saturation = 1;
    int Brightness = 1;
    newrect.setFill(Color.hsb(Hue, Saturation, Brightness));


         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 color by HSB values ");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill it using HSB values 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.Color, 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 Colored Rectangle specified using HSB values, 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 color of the Rectangle. Hue, Saturation, Brightness values are set and passed in the hsb() method of Color class which 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 color by HSB values ”. Also, it displays a rectangle with defined x, y coordinates, and width, height with HSB values color filled in it.

JavaFX Color

Filling the color by specifying Web color in JavaFX:

Example:

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


  




public class ColorUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Rectangle newrect = new Rectangle(); 
    newrect.setX(80); 
    newrect.setY(80); 
    newrect.setWidth(200); 
    newrect.setHeight(200);
   
    newrect.setFill(Color.web("#FF0000"));


         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 color by using web color values ");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Rectangle shape and fill it in using web 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.Color, 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 Rectangle filled using web color, 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 color of the Rectangle. The web() method of a color class is called with color values passed in it which are again 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 color by using web color values ”. Also, it displays a rectangle with defined x, y coordinates, and width, height with specified web color filled in it.

JavaFX Color

Related Topics

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 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 Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 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 Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 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 Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Color Input

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

4 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 Pause Transition

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

4 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 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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Fill Transition

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

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.