The Font Class

paulike

Paul Ngugi

Posted on June 20, 2024

The Font Class

A Font describes font name, weight, and size. You can set fonts for rendering the text. The javafx.scene.text.Font class is used to create fonts, as shown in Figure below.

Image description

A Font instance can be constructed using its constructors or using its static methods. A Font is defined by its name, weight, posture, and size. Times, Courier, and Arial are the examples of the font names. You can obtain a list of available font family names by invoking the static getFamilies() method. List is an interface that defines common methods for a list. ArrayList is a concrete implmentation of List. The font postures are two constants: FontPosture.ITALIC and FontPosture.REGULAR. For example, the following statements create two fonts.

Font font1 = new Font("SansSerif", 16);
Font font2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 12);

The program below gives a program that displays a label using the font (Times New Roman, bold, italic, and size 20).

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.*;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class FontDemo extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Create a pane to hold the circle
        Pane pane = new StackPane();

        // Create a circle and set its properties
        Circle circle = new Circle();
        circle.setRadius(50);
        circle.setStroke(Color.BLACK);
        circle.setFill(new Color(0.5, 0.5, 0.5, 0.1));
        pane.getChildren().add(circle); // Add circle to the pane

        // Create a label and set its properties
        Label label = new Label("JavaFX");
        label.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 20));
        pane.getChildren().add(label);
//      pane.getChildren().addAll(circle, label);

        // Create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setTitle("FontDemo"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

Enter fullscreen mode Exit fullscreen mode

Image description

The program creates a StackPane (line 15) and adds a circle and a label to it (lines 22, 27). These two statements can be combined using the following one statement:

pane.getChildren().addAll(circle, label);

A StackPane places the nodes in the center and nodes are placed on top of each other. A custom color is created and set as a fill color for the circle (line 21). The program creates a label and sets a font (line 26) so the text in the label is displayed in Times New Roman, bold, italic, and 20 pixels.

As you resize the window, the circle and label are displayed in the center of the window, because the circle and label are placed in the stack pane. Stack pane automatically places nodes in the center of the pane.

A Font object is immutable. Once a Font object is created, its properties cannot be changed.

💖 💪 🙅 🚩
paulike
Paul Ngugi

Posted on June 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related