Changing Text Fonts with code - Java JavaFX

Java examples for JavaFX:Text

Description

Changing Text Fonts with code

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.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    Application.launch(args);/*from  w w  w.  j  a va 2s  . co  m*/
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Changing Text Fonts");
    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);
    root.getChildren().add(my2);

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

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

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

    Reflection refl = new Reflection();
    refl.setFraction(0.8f);
    my5.setEffect(refl);

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

Related Tutorials