In JavaFX we can apply colors (Paint) to objects.
In JavaFX, all shapes can be filled with simple colors and gradient colors.
When specifying color values, we can use the colors in the default RGB color space.
To create a color, use the Color.rgb() method. This method takes three integer values, representing red, green, and blue components.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; /*from w ww . j a v a 2 s .c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Drawing Text"); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); int x = 100; int y = 100; int red = 30; int green = 40; int blue = 50; Text text = new Text(x, y, "JavaFX 2.0"); text.setFill(Color.rgb(red, green, blue, .99)); text.setRotate(60); root.getChildren().add(text); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
The following code creates color from color name. Color.DARKBLUE
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; // w ww . ja v a 2 s. co m public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Title"); final Circle circ = new Circle(40, 40, 30); final Group root = new Group(circ); final Scene scene = new Scene(root, 800, 400, Color.BEIGE); final Text text1 = new Text(25, 25, "java2s.com"); text1.setFill(Color.DARKBLUE); text1.setFont(Font.font(java.awt.Font.SERIF, 25)); root.getChildren().add(text1); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
Another overloaded method takes three integer values and a fourth double type value which is the alpha channel.
The fourth value sets the opacity of the color. This value is between zero (0) and one (1).
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; //from ww w . ja v a2 s . c o m public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Text Fonts"); Group root = new Group(); Scene scene = new Scene(root, 550, 250, new Color(0,0,1,1.0)); Text text = new Text(50, 100, "JavaFX 2.0 from Java2s.com"); Font sanSerif = Font.font("Dialog", 30); text.setFont(sanSerif); text.setFill(Color.RED); root.getChildren().add(text); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
We can also create color by specifying hue, saturation, and brightness(HSB).
To create a color using HSB, use the Color.hsb()
method.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; //from ww w .j av a 2 s . c o m public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Text Fonts"); Group root = new Group(); Scene scene = new Scene(root, 550, 250,Color.hsb(270,1.0,1.0,1.0)); Text text = new Text(50, 100, "JavaFX 2.0 from Java2s.com"); Font sanSerif = Font.font("Dialog", 30); text.setFont(sanSerif); text.setFill(Color.RED); root.getChildren().add(text); primaryStage.setScene(scene); primaryStage.show(); } }
The code above generates the following result.
The following code shows how to create color from web value.
Color c = Color.web("#0000FF",1.0);// blue as a hex web value, explict alpha Color c = Color.web("#0000FF");// blue as a hex web value, implict alpha Color c = Color.web("0000FF",1.0);// blue as a hex web value, explict alpha Color c = Color.web("0000FF");// blue as a hex web value, implict alpha
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; /*from ww w . j a v a2 s. c om*/ public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search"); label1.setTextFill(Color.web("#0076a3")); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
To specify color values using RGB hexadecimal values as CSS, we can use the Color.web() method.
The code above generates the following result.