How to add a combination of multiple effects to text in JavaFX?

JavaFXObject Oriented ProgrammingProgramming

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

A blend effect is the one that blends two inputs together. The javafx.scene.effect.Blend class represents the blend effect. To add the blend effect to a text node −

  • Instantiate the Text class bypassing basic the x, y coordinates (position), and text string as arguments to the constructor.

  • Set desired properties like font, stoke, etc.

  • Instantiate the Blend class.

  • Set the blend mode using the setMode() method.

  • Create two different inputs by applying effects or changing colors.

  • Set the inputs to the blend using the setBottomInput() and setTopInput() methods.

  • Set the created effect to the text node using the setEffect() method.

  • 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.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class CombiningEffects extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      String str = "Tutorialspoint";
      Text text = new Text(30.0, 100.0, str);
      //Setting the font
      Font font = Font.font("Brush Script MT", FontWeight.BOLD, 110);
      text.setFont(font);
      //Setting color of the text
      text.setFill(Color.BLUEVIOLET);
      //Creating the drop shadow effect
      DropShadow dropShadow = new DropShadow();
      dropShadow.setOffsetY(10);
      text.setEffect(dropShadow);
      //Creating the inner shadow effect
      InnerShadow innerShadow = new InnerShadow();
      innerShadow.setOffsetX(8.0);
      innerShadow.setOffsetY(8.0);
      //Creating the blend
      Blend blend = new Blend();
      blend.setMode(BlendMode.ADD);
      //Setting both the shadow effects to the blend
      blend.setBottomInput(dropShadow);
      blend.setBottomInput(innerShadow);
      //Setting the effect to the text
      text.setEffect(blend);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Blending Effects");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 16-May-2020 09:30:02
Advertisements