Home > Java > javaTutorial > How to add blur effect to text nodes in JavaFX?

How to add blur effect to text nodes in JavaFX?

王林
Release: 2023-08-19 12:05:34
forward
1529 people have browsed it

You can use the setEffect() method to add effects to any node object in JavaFX. This method accepts an object of class Effect and adds it to the current node.

javafx.scene.effect.GaussianBlur.GaussianBlur class represents the blur effect that uses Gaussian convolution kernel internally. So, to add a blur effect to a text node:

  • Instantiate the Text class by passing the base x, y coordinates (position) and a text string as arguments to the constructor.

  • Set the required properties, such as font, stroke, etc.

  • Create a blur effect by instantiating the GaussianBlur class.

  • Use the setEffect() method to set the created effect to the text node.

  • Finally, add the created text node to the Group object.

Example

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.GaussianBlur;
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 TextBlurEffect 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 and color of the stroke
      text.setStrokeWidth(2);
      text.setStroke(Color.BLUE);
      //Setting the blur effect to the text
      GaussianBlur blur = new GaussianBlur();
      text.setEffect(blur);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Blur Effect");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}
Copy after login

Output

How to add blur effect to text nodes in JavaFX?

The above is the detailed content of How to add blur effect to text nodes in JavaFX?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template