Changing Text Fonts Using TextFlow - Java JavaFX

Java examples for JavaFX:Text

Description

Changing Text Fonts Using TextFlow

Demo Code

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    Application.launch(args);//from   w ww  .  j a v  a2 s  . c  om
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 330, 250, Color.WHITE);

    // Serif with drop shadow
    Text my2 = new Text(50, 50, "test");
    Font serif = Font.font("Serif", 30);
    my2.setFont(serif);
    my2.setFill(Color.RED);
    DropShadow dropShadow = new DropShadow();
    dropShadow.setOffsetX(2.0f);
    dropShadow.setOffsetY(2.0f);
    dropShadow.setColor(Color.rgb(50, 50, 50, .588));
    my2.setEffect(dropShadow);

    // SanSerif
    Text my3 = new Text(50, 100, "test\n");
    Font sanSerif = Font.font("SanSerif", 30);
    my3.setFont(sanSerif);
    my3.setFill(Color.BLUE);

    // Dialog
    Text my4 = new Text(50, 150, "test\n");
    Font dialogFont = Font.font("Dialog", 30);
    my4.setFont(dialogFont);
    my4.setFill(Color.rgb(0, 255, 0));

    // Monospaced
    Text my5 = new Text(50, 200, "test");
    Font monoFont = Font.font("Monospaced", 30);
    my5.setFont(monoFont);
    my5.setFill(Color.BLACK);

    Reflection refl = new Reflection();
    refl.setFraction(0.8f);
    my5.setEffect(refl);
    TextFlow flow = new TextFlow(my2, my3, my4,
        my5);

    root.getChildren().add(flow);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

Related Tutorials