We know that AWT stands for ABSTRACT WINDOW TOOLKIT. For, Example when we want to draw a string in the AWT in Frame we use the method Graphics.drawString(String str, int x_coordinate, int y_coordinate). Now use the Font class for the string to display in the type of font you want. As it is difficult to remember all the font names we can get the font names that are available in the AWT with the help of GraphicsEnvironment class in AWT.
After importing the GraphicsEnvironment in the AWT package in java. we need to import the getLocalGraphicsEnvironment() In the getLocalGraphicsEnvironment() we have to create an object for that and with the help of the object we have to call the getAvailableFontFamilyNames().
Program to Fetch Font Family Names in the AWT
By using getAvailableFontFamilyNames() you can get the array of all the available fonts in AWT, which have been displayed on the console.
// Java Program to extract the list of Fonts import java.awt.*;
// Driver class to check available Fonts in AWT class GFG {
// Main Function
public static void main(String[] args)
{
System.out.println( "To Know the available font family names" );
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println( "Getting the font family names" );
// Array of all the fonts available in AWT
String fonts[] = ge.getAvailableFontFamilyNames();
// Getting the font family names
for (String i : fonts) {
System.out.println(i + " " );
}
}
} |
Output:
Explanation of the above Program:
- Using the instance of GraphicsEnvironment.getLocalGraphicsEnvironment().
- Fetching the array of Available fonts into the String fonts[] with the help of getAvailableFontFamilyNames().
Now we can use any of the font family names and we can use it in the Font class that is present in the AWT package in Java.
Usage of font class with the help of font family names in AWT
Now let’s see an example program to learn about the usage of the font family in Java.awt package.
// Java program to test // Custom available font in AWT import java.awt.*;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GFG extends Frame {
//class to close the AWT frame
GFG(){
// To close the AWT frame we use the WindowAdapter class
this .addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit( 0 );
}
});
}
// Here to draw the string we use the
// Graphics class in java
public void paint(Graphics g){
g.setColor(Color.GREEN);
// Algerian font family is present in the above
// mentioned program output
g.setFont( new Font( "Algerian" , Font.BOLD, 50 ));
g.drawString( "GEEKS FOR GEEKS" , 100 , 200 );
}
//Main Method
public static void main(String args[]){
GFG obj = new GFG();
obj.setTitle( "GeeksForGeeks" );
obj.setSize( 600 , 600 );
obj.setVisible( true );
}
} |
Output:
This is about Knowing the FontFamily in AWT and using it in the program for drawing a string in the AWT.
Explanation of the above Program:
- GFG obj to create a window layout to display data.
- Set the different parameters like Title, Size, Visible, etc.
- paint() method to create the Graphics of the text “Geeks For Geeks”.