How to add LCD (liquid crystal display) to text in JavaFX?


The javafx.scene.text.Text class has a property named fontSmoothingType, which specifies the smoothing type of the text. You can set the value to this property using the set setFontSmoothingType() method accepts two parameters −

  • FontSmoothingType.GRAY − This specifies the default grayscale smoothing.

  • FontSmoothingType.LCD − This specifies the LCD smoothing. This uses the characteristics of an LCD display and enhances the smoothing of the node.

To add an LCD display to a text −

  • Create a text node by instantiating the javafx.scene.text.Text class.

  • Create a required font using one of the font() methods of the javafx.scene.text.Font class.

  • Set the font to the text using the setText() method.

  • Set the LCD smoothing type to the text by passing the FontSmoothingType.LCD as a parameter to the setFontSmoothingType() method.

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.FontSmoothingType;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class LCDTextExample 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);
      //Setting the liquid crystal display to the text
      text.setFontSmoothingType(FontSmoothingType.LCD);
      //Setting the color of the text
      text.setFill(Color.BROWN);
      //Setting the width and color of the stroke
      text.setStrokeWidth(1);
      text.setStroke(Color.DARKRED);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Liquid Crystal Display");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 16-May-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements