
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to add stroke and color to text in JavaFX?
Since the javafx.scene.text.Text class in JavaFX inherits the Shape class it inherits all its members. You can modify the stroke and color of the text node by setting values to the stroke, stroke width and fill properties inherited by the Text class.
Stroke Width − The stroke width property specifies/defines the width of the boundary line of a shape. You can set value to the width of the boundary using the setWidth() method of the Shape class.
Fill − The fill property specifies/defines the color with which the interior area of the shape is to be filled. You can fill a particular shape with desired color using the fill() method of the Shape class.
Stroke − The stroke property specifies/defines the color of the boundary of a shape. You can set the color of the boundary using the setStroke() method of the javafx.scene.shape.Shape class.
Example
import java.io.FileNotFoundException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class SettingStroke_Color extends Application { public void start(Stage stage) throws FileNotFoundException { //Creating a text object String str = "Welcome to Tutorialspoint"; Text text = new Text(30.0, 80.0, str); //Setting the font Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65); text.setFont(font); //Setting the color of the text text.setFill(Color.BROWN); //Setting the width text.setStrokeWidth(2); //Setting the stroke color text.setStroke(Color.BLUE); //Setting the stage Group root = new Group(text); Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Stroke And Color"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to add stroke to Text using FabricJS?
- How to add various fonts to text using text flow in JavaFX?
- How to add custom fonts to a text in JavaFX?
- How to add LCD (liquid crystal display) to text in JavaFX?
- How to add a blur effect to a text node in JavaFX?
- How to add a reflection effect to a text node in JavaFX?
- How to add a combination of multiple effects to text in JavaFX?
- How to add a drop shadow effect to a text node in JavaFX?
- How to add an inner shadow effect to a text node in JavaFX?
- How to add stroke to IText using FabricJS?
- How to strike through and underline text in JavaFX?
- How to add stroke to an Ellipse using FabricJS?
- How to add stroke to a Circle using FabricJS?
- How to add stroke to a Triangle using FabricJS?
- How to add stroke to a Rectangle using FabricJS?
