Java Retrieve Font Information

Java Retrieve Font Information

Java Retrieve Font Information

Summary: By the end of this tutorial "Java Retrieve Font Information", you will be comfortable to retrieve the components of Font object like font name, style and size.

The Font object (discussed earlier) comprises of three components – font name as string, font style and font size as integer values. We can retrieve these three components from a Font object.

Supporting Methods of java.awt.Font class
  • int getName(): Returns the font name (of the font object) as string.
  • int getStyle(): Returns the font style as an integer value.
  • int getSize(): Returns the font size as an integer value.
  • String getFamily(): Returns the font family, as string, for which the font belongs.
Supporting Method of java.awt.Graphics class
  • Font getFont(): Returns a Font object that represents current font or active font of Graphics object.

The following program uses the all the above five methods.


Java Retrieve Font Information
Output screen of GetFontInfo.java of Java Retrieve Font Information

The methods f1.getName(), f1.getStyle(), f1.getSize() and f1.getFamily() return the font name, style, size and family of the Font object f1.

g.getFont();

getFont() is a method of Graphics class and returns the active font particulars as an object of Font. If no font is set with setFont() method, the default font is returned. Observe the screenshot for the default font particulars.

Leave a Reply

Your email address will not be published. Required fields are marked *