Java Applet program that shows all available fonts using awt and applet package

This Applet program extracts all available fonts and prints them in a new window. It uses two packages named applet and awt, can be imported using the following code snippet.

import java.applet.*;
import java.awt.*;

A java class to be created extending Applet class and we need to override only paint method so a public void method named, paint(Graphics g), to be created.

public class allAvailableFontFamily extends Applet{
  public void paint(Graphics g){
    
  }
}

setting local Graphics environment inside the paint method

			GraphicsEnvironment env;
			env=GraphicsEnvironment.getLocalGraphicsEnvironment();

all font-family names to be extracted and stored in a string array

			String allfonts[];
			allfonts=env.getAvailableFontFamilyNames();

After extracting them, we might iterate and set font one by one and print their names in their own style.

			//printing extracted data
			int i,x=20,y=20;
			for(i=0;i<allfonts.length;i++){
              	//creating Font instance with one of the extracted font family name
				Font f=new Font(allfonts[i],Font.BOLD,20);
              	//setting the font
				g.setFont(f);
              	//printing the font name in its style on output window
				g.drawString(allfonts[i],x,y);
				y+=20;
              	//channging column
				if(i%41==40){
					x+=300;
					y=20;
				}
			}

So, As a whole the program is

import java.applet.*;
import java.awt.*;
public class allAvailableFontFamily extends Applet{
	//overriding paint method
	public void paint(Graphics g){
      		//creating GraphicsEnvironment instance
			GraphicsEnvironment env;
			env=GraphicsEnvironment.getLocalGraphicsEnvironment();
			String allfonts[];
      		//extracting all availble font family names
			allfonts=env.getAvailableFontFamilyNames();
      		//printing extracted data
			int i,x=20,y=20;
			for(i=0;i<allfonts.length;i++){
              //creating Font instance with one of the extracted font family name
				Font f=new Font(allfonts[i],Font.BOLD,20);
              //setting the font
				g.setFont(f);
              //printing the font name in its style on output window
				g.drawString(allfonts[i],x,y);
				y+=20;
              //channging column
				if(i%41==40){
					x+=300;
					y=20;
				}
			}
		}	
}
<html>
<body>
<applet code="allAvailableFontFamily.class" width="640" height="480">
</applet>
</body>
</html>

Instruction to Run: if you have applet viewer in your installed java version then simply run the following command.

appletviewer html_filename.html

output: opens in a new window

all available font family

Share on Social Media
Mukuldeep Maiti

Mukuldeep Maiti

An unrecognized crazy being with random thoughts lost in unknown

You may also like...

Leave a Reply