Updated March 18, 2023
Introduction to JavaFX Color
In JavaFX, color can be used to fill the different shapes such as rectangle, ellipse, circle, etc. By using different methods, it is possible to make our shades of color. Once it is made, it can be passed to the object of paint into the method setFill(). In this document, we will be discussing several techniques to create color.
How to Create Color in JavaFX?
As already said, colors can be made using different methods:
1. Using the Name of Color
In this method, the color name will be used to create a color. It is done with the help of class javafx.scene.paint.Color where all colors are available as properties of the class. Color name can be passed to the object of Paint class into the method setFill(). Here is an example of creating color using a color name.
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a group gp
Group gp = new Group();
//set the title
s.setTitle("Color sample using color name");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width and height of rectangle r1
r1.setWidth(110);
r1.setHeight(140);
//set the color as red by passing color name
r1.setFill(Color.RED);
//set an effect
r1.setEffect(new DropShadow());
//create a rectangle r2
Rectangle r2 = new Rectangle();
//set the x coordinate of rectangle r2
r2.setX(60);
//set the x coordinate of rectangle r2
r2.setY(60);
//set the width of rectangle r2
r2.setWidth(100);
//set the height of rectangle r2
r2.setHeight(150);
//set the color as GREEN by passing color name
r2.setFill(Color.GREEN);
//set an effect
r2.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
gp.getChildren().add(r2);
//create a scene sc
Scene sc = new Scene(gp,700,450);
//set the scene for the stage
s.setScene(sc);
//display the results
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
2. Using Web Color
The next method for creating color is by using a web color. Here, Color.web() method in class javafx.scene.paint.color will be used where 2 parameters will be passed such as color’s hex value and an alpha channel. The second parameter Alpha-channel is an optional parameter that denotes the color’s opacity. Alpha has a range of values 0.0 to 1.0 and also, it can be implicit or explicit as shown below.
Syntax:
//Red color and Alpha is implicit
Color.web("#ff0000")
//Red color and Alpha is explicit
Color.web("#ff0000",1)
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using web color");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width of rectangle r1
r1.setWidth(100);
//set the height of rectangle r1
r1.setHeight(150);
//set the color of rectangle r1 as red by using color.web method
r1.setFill(Color.web("#ff0000",1));
//set an effect
r1.setEffect(new DropShadow());
//create a rectangle r2
Rectangle r2 = new Rectangle();
//set the x coordinate of rectangle r2
r2.setX(60);
//set the x coordinate of rectangle r2
r2.setY(60);
//set the width of rectangle r2
r2.setWidth(100);
//set the height of rectangle r2
r2.setHeight(150);
//set the color of rectangle r2 as black by using color.web method
r2.setFill(Color.web("#000000",1));
//set an effect
r2.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
gp.getChildren().add(r2);
//create a scene sc
Scene sc = new Scene(gp,700,450);
//set the scene for the stage
s.setScene(sc);
//display the results
s.show();
}
public static void main(String[] args) {
launch(args); }}
Output:
3. Using HSB Color
Color can also be created by using Hue, Saturation and Brightness combination which is known as HSB color. It is done with the help of class javafx.scene.paint.Color that consists of a method Color.hsb() that inputs 3 integers such as h,s, and b.
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using HSB");
//create a rectangle r1
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width of rectangle r1
r1.setWidth(100);
//set the height of rectangle r1
r1.setHeight(150);
//set an effect
r1.setEffect(new DropShadow());
//add children to the group
gp.getChildren().add(r1);
//create a scene sc
Scene sc = new Scene(gp,700,450,Color.hsb(180, 0, 1));
//set the scene
s.setScene(sc);
//display the results
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
4. Using RGB Color
One of the most common methods to create color is RGB color system where Red, Green, and Blue are the 3 components. It is done with the help of class javafx.scene.paint.Color that consists of a method rgb() that inputs 3 integers r,g, and b.
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
//class that extends the application class
public class JavaFXColorExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a group gp
Group gp = new Group();
//set the title of the stage s
s.setTitle("Color sample using RGB");
//create a rectangle r
Rectangle r1 = new Rectangle();
//set the x coordinate of rectangle r1
r1.setX(50);
//set the x coordinate of rectangle r1
r1.setY(20);
//set the width and height of rectangle r1
r1.setWidth(100);
r1.setHeight(140);
r1.setFill(Color.rgb(20, 125, 10, 0.63));
//add children to the group
gp.getChildren().add(r1);
//create a scene sc
Scene sc = new Scene(gp,700,450);
//set the scene
s.setScene(sc);
//display the results
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
Conclusion
Colors are used to fill the shapes and it can be done using different methods. All these methods are addressed in this document.
Recommended Articles
This is a guide to JavaFX Color. Here we discuss to Create Color in JavaFX Using Various Methods along with Code Implementation and Output. you can also go through our suggested articles to learn more –