কম্পিউটার টিউটোরিয়াল

জাভাFX-এ টেক্সট নোডে ফন্ট সেট করা

টেক্সট নোডে ফন্ট সেট করার পদ্ধতি

জাভাFX-এ টেক্সট নোড রিপ্রেজেন্ট করা হয় javafx.scene.text.Text ক্লাস দিয়ে। ডিফল্টভাবে টেক্সট নিচের মতো দেখায়:

জাভাFX-এ টেক্সট নোডে ফন্ট সেট করা

স্বচ্ছন্দ ফন্ট সেট করা

setFont() মেথড দিয়ে টেক্সট নোডে ফন্ট সেট করা যায়। এটি javafx.scene.text.Font ক্লাসের অবজেক্ট গ্রহণ করে।

Font ক্লাসে font() মেথডের অনেক ভারিয়েন্ট রয়েছে:

font(double size)
font(String family)
font(String family, double size)
font(String family, FontPosture posture, double size)
font(String family, FontWeight weight, double size)
font(String family, FontWeight weight, FontPosture posture, double size)

এখানে,

  • size (double) - ফন্টের আকার।
  • family (String) - ফন্ট ফ্যামিলির নাম। ইনস্টল্ড ফন্ট ফ্যামিলির নাম পেতে getFamilies() মেথড ব্যবহার করা হয়।
  • weight - ফন্টের ওজন (FontWeight Enum: BLACK, BOLD, EXTRA_BOLD, EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN)।
  • posture - ফন্টের পজিশন (FontPosture Enum: REGULAR, ITALIC)।

সব মেথড স্ট্যাটিক এবং Font অবজেক্ট রিটার্ন করে।

টেক্সট নোডে ফন্ট সেট করার ধাপ

  1. Text ক্লাস ইনস্ট্যানশিয়েট করুন।
  2. সেটার মেথড বা কনস্ট্রাক্টর দিয়ে পজিশন এবং টেক্সট স্ট্রিং সেট করুন।
  3. Font অবজেক্ট তৈরি করুন।
  4. setFont() দিয়ে ফন্ট সেট করুন।
  5. Group অবজেক্টে নোড যোগ করুন।

উদাহরণ

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.InputStream; 
import java.util.Scanner; 
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.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 

public class SettingFont extends Application { 
    public void start(Stage stage) throws FileNotFoundException { 
        // Reading the contents of a text file. 
        InputStream inputStream = new FileInputStream("D:\\sample.txt"); 
        Scanner sc = new Scanner(inputStream); 
        StringBuffer sb = new StringBuffer(); 
        while(sc.hasNext()) { 
            sb.append(" " + sc.nextLine() + "\n"); 
        } 
        String str = sb.toString(); 
        // Creating a text object 
        Text text = new Text(); 
        // Setting the basic properties of text 
        text.setText(str); 
        text.setX(10.0); 
        text.setY(25.0); 
        text.setWrappingWidth(580); 
        // Creating the font object 
        String font_name = Font.getFamilies().get(25); 
        System.out.println("Font Name:" + font_name); 
        int size = 25; 
        Font font = Font.font(font_name, FontWeight.BOLD, FontPosture.REGULAR, size); 
        // Setting font to the text 
        text.setFont(font); 
        // Setting the stage 
        Group root = new Group(text); 
        Scene scene = new Scene(root, 595, 300, Color.BEIGE); 
        stage.setTitle("Displaying Text"); 
        stage.setScene(scene); 
        stage.show(); 
    } 
    public static void main(String args[]){ 
        launch(args); 
    } 
}

sample.txt কনটেন্ট

JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc.. To develop GUI Applications using Java programming language, the programmers rely on libraries such as Advanced Windowing Tool kit and Swing. After the advent of JavaFX, these Java programmers can now develop GUI applications effectively with rich content.

আউটপুট

Font Name: Brush Script MT

জাভাFX-এ টেক্সট নোডে ফন্ট সেট করা

  1. জাভाएফএক্স-এ টেক্সট নোডে ব্লার ইফেক্ট যোগ করার পদ্ধতি

  2. Tkinter Canvas টেক্সট আইটেমের ফন্ট সাইজ সেট করা

  3. Tkinter-এ টেক্সটের জন্য ফন্ট সেট করা

  4. এন্ড্রয়েডে কাস্টম ফন্ট সেট করার পদ্ধতি