Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJavaFX | Font Class

JavaFX | Font Class

Font class is a part of JavaFX. The Font class represents fonts, which are used to render text on the screen. The size of a Font is described as being specified in points which are a real world measurement of approximately 1/72 inch. Font class inherits Object class.

Constructors of the class:

  1. Font(double s) : Creates a font object with specified size.
  2. Font(String n, double s) : Creates a font object with specified name and size.

Commonly Used Methods:

Method Explanation
font(double s) Creates a font object with specified size.
font(String f) Creates a font object with specified family name.
font(String f, double s) Creates a font object with specified family name and size.
font(String f, FontPosture p, double s) Creates a font object with specified family name, posture and size.
font(String f, FontWeight weight, double s) Creates a font object with specified family name, fontweight and size.
font(String f, FontWeight w, FontPosture p, double s) Creates a font object with specified family name, fontweight, font posture and size.
getDefault() Returns the default font.
getFamilies() Gets all the font families installed on the user’s system.
getFamily() Returns the family of the font.
getFontNames() Gets the names of all fonts that are installed on the users system.
getFontNames(String f) Gets the names of all fonts in the specified font family that are installed on the users system.
loadFont(InputStream in, double s) Loads a font resource from the specified input stream.
loadFont(String url, double s) Loads a font resource from the specified URL.
getName() The full font name.
getSize() The point size for this font.

Below programs illustrate the use of Font Class:

  1. Java Program to create a font object and apply it to a text: In this program we will create a Font named font and specify its family, its font weight and size. Apply this font to the text and add this text to the TextFlow named textflow. Create a VBox named vbox and add the textflow to the vbox and add the vbox to the scene and add the scene to stage. Call the show() function to display the results.




    // Java Program to create a font object 
    // and apply it to a text
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class Font_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("Font");
      
                // create TextFlow
                TextFlow text_flow = new TextFlow();
      
                // create text
                Text text_1 = new Text("Lazyroar\n");
      
                // set the text color
                text_1.setFill(Color.GREEN);
      
                // create a font
                Font font = Font.font("Verdana", FontWeight.EXTRA_BOLD, 25);
      
                // set font of the text
                text_1.setFont(font);
      
                // set text
                text_flow.getChildren().add(text_1);
      
                // set line spacing
                text_flow.setLineSpacing(20.0f);
      
                // create VBox
                VBox vbox = new VBox(text_flow);
      
                // set alignment of vbox
                vbox.setAlignment(Pos.CENTER);
      
                // create a scene
                Scene scene = new Scene(vbox, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  2. Java Program to create a font object and apply it to a text and allow the user to select the font from the combo box: In this program we will create a Font named font and specify its family, its font weight and size. Apply this font to the text and add this text to the TextFlow named textflow. Create a VBox named vbox and add the textflow to the vbox and add the vbox to the scene and add the scene to stage. Create two combo box and add the font names to one and the font weight to the other and create an EventHandler to handle the events of the combo boxes and set the font to the type specified by the user.




    // Java Program to create a font object 
    // and apply it to a text and allow the 
    // user to select font from the combo box
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
    import javafx.collections.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
       
    public class Font_2 extends Application {
       
    // launch the application
    public void start(Stage stage)
    {
      
    try {
      
        // set title for the stage
        stage.setTitle("Font");
      
        // create TextFlow
        TextFlow text_flow = new TextFlow();
      
        // create text
        Text text_1 = new Text("Lazyroar\n");
      
        // set the text color
        text_1.setFill(Color.GREEN);
      
        // create a font
        Font font = Font.font(Font.getFontNames().get(0), 
                              FontWeight.EXTRA_BOLD, 20);
      
        // font weight names
        String weight[] = { "BLACK", "BOLD",
                            "EXTRA_BOLD",
                            "EXTRA_LIGHT",
                            "LIGHT",
                            "MEDIUM",
                            "NORMAL",
                            "SEMI_BOLD",
                            "THIN" };
      
        // Create a combo box
        ComboBox combo_box =
          new ComboBox(FXCollections.observableArrayList(weight));
      
        // Create a combo box
        ComboBox combo_box1 = 
          new ComboBox(FXCollections.observableArrayList(Font.getFontNames()));
      
        // Create action event
        EventHandler<ActionEvent> event =
        new EventHandler<ActionEvent>() {
      
            public void handle(ActionEvent e)
            {
      
                // set font of the text
                text_1.setFont(Font.font((String)combo_box1.getValue(),
                     FontWeight.valueOf((String)combo_box.getValue()), 20));
            }
        };
      
        // Create action event
        EventHandler<ActionEvent> event1 = 
           new EventHandler<ActionEvent>() {
      
            public void handle(ActionEvent e)
            {
      
                // set font of the text
                text_1.setFont(Font.font((String)combo_box1.getValue(),
                      FontWeight.valueOf((String)combo_box.getValue()), 20));
            }
        };
      
        // Set on action
        combo_box.setOnAction(event);
        combo_box1.setOnAction(event1);
      
        // set font of the text
        text_1.setFont(font);
      
        // set text
        text_flow.getChildren().add(text_1);
      
        // set line spacing
        text_flow.setLineSpacing(20.0f);
      
        // create a HBox
        HBox hbox = new HBox(combo_box, combo_box1);
      
        // create VBox
        VBox vbox = new VBox(hbox, text_flow);
      
        // set spacing
        vbox.setSpacing(30.0);
      
        // set alignment of vbox
        vbox.setAlignment(Pos.CENTER);
      
        // create a scene
        Scene scene = new Scene(vbox, 400, 300);
      
        // set the scene
        stage.setScene(scene);
      
        stage.show();
    }
      
    catch (Exception e) {
      
        System.out.println(e.getMessage());
    }
    }
      
    // Main Method
    public static void main(String args[])
    {
      
        // launch the application
        launch(args);
    }
    }

    
    

    Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/Font.html

RELATED ARTICLES

Most Popular

Recent Comments