- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add colors to nodes in JavaFX?
You can apply colors to nodes in JavaFX using the setFill() and setStroke() methods. The setFill() method adds color to the surface area of the node whereas the setStroke() method applies color to the boundary of the node.
Both methods accept an object of the javafx.scene.paint.Paint class as a parameter. It is the base class for the color and gradients that are used to fill the shapes and backgrounds with color.
The javafx.scene.paint.Color class in JavaFX is a subclass of the Paint and it encapsulates all the colors in RGB color space (as its properties).
To apply color to a geometrical shape or background or the stroke of shape, you need to invoke the respective method by passing a Color object representing the desired color.
Example
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; public class ColorExample extends Application { public void start(Stage stage) { //Drawing a circle Circle circle = new Circle(75.0f, 65.0f, 40.0f ); //Drawing a Rectangle Rectangle rect = new Rectangle(150, 30, 100, 65); //Drawing an ellipse Ellipse ellipse = new Ellipse(330, 60, 60, 35); //Drawing Polygon Polygon poly = new Polygon(410, 60, 430, 30, 470, 30, 490, 60, 470, 100, 430, 100 ); //Color by name circle.setFill(Color.CHOCOLATE); circle.setStrokeWidth(5); circle.setStroke(Color.DARKRED); //Using RGB rect.setFill(Color.rgb(150, 0, 255)); rect.setStrokeWidth(5); rect.setStroke(Color.DARKRED); //Using HSB ellipse.setFill(Color.hsb(50, 1, 1)); ellipse.setStrokeWidth(5); ellipse.setStroke(Color.DARKRED); //Using web poly.setFill(Color.web("#E9967A")); poly.setStrokeWidth(5); poly.setStroke(Color.DARKRED); //Setting the stage Group root = new Group(circle, ellipse, rect, poly); Scene scene = new Scene(root, 600, 150); stage.setTitle("Setting Colors"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
Advertisements