Way2Java

class Toolkit Get Font List

class Toolkit Get Font List

Summary: In this tutorial "class Toolkit Get Font List", explained usage of ToolKit class to get font list supported by Java graphics.

java.awt.Toolkit is an abstract class and can be used by the programmer to interact with the operating system to know some data supported by the OS. In the program, we used to know the font names supported which can be used in the program to create a font object.

Following is the class signature

public abstract class Toolkit extends Object
Program printing font list
import java.awt.*;
public class JavaFontNames extends Frame
{
  public JavaFontNames()
  {
    super("Knowing Font Names");
    setSize(350, 200);
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawString("Following is the list of Logical font names supported by Java:", 40, 50);
                                          // to obtain the font list
    Toolkit tk = Toolkit.getDefaultToolkit();
    String names[] = tk.getFontList();

    for(int i=0, y=85; i < names.length; i++, y += 20)
    {
      g.drawString(names[i], 60, y);
    }
  }
  public static void main(String args[])
  {
    new JavaFontNames();
  }
}


Output screen JavaFontNames.java of class Toolkit Get Font List
                Toolkit tk = Toolkit.getDefaultToolkit();
                String names[] = tk.getFontList();

Toolkit is an abstract class and its object cannot be created directly. We require a factory method to do the job. The factory method here is getDefaultToolkit() which returns an object of Toolkit class. Now tk works like an object for all practical purposes. With tk, we can call all the methods of Toolkit class.

Similarly you can get color components information by using Color class methods.