/*
Get Font Information Example
This java example shows how to get Font information like font name,
size, style using Java AWT Font class.
*/
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
/*
<applet code="GetFontInformation" width=200 height=200>
</applet>
*/
public class GetFontInformation extends Applet{
public void paint(Graphics g){
/*
* To get current font, use
* Font getFont() method.
*/
Font currentFont = g.getFont();
/*
* To get a font name, use
* String getName()
* method of AWT Font class.
*/
String fontName = currentFont.getName();
/*
* To get font size, use
* int getSize()
* method of Font class.
*/
int size = currentFont.getSize();
/*
* To get style of font, use
* int getStyle()
* method of Font class.
*
* Style can be Font.BOLD, Font.ITALIC or Font.PLAIN.
*
* You can directly call isBold(), isItalic() or
* isPlain() methods of AWT Font class.
*/
int style = currentFont.getStyle();
String fontStyle = "";
if( (style & Font.BOLD) == Font.BOLD)
fontStyle = "Bold";
if( (style & Font.ITALIC) == Font.ITALIC)
fontStyle = "Italic";
if( (style & Font.PLAIN) == Font.PLAIN)
fontStyle = "Plain";
/*
* To get font family, use
* String getFamily()
* method of Font class.
*/
String family = currentFont.getFamily();
g.drawString("Font Name: " + fontName, 10, 30);
g.drawString("Font size: " + size, 10, 50);
g.drawString("Font Family: " + family, 10, 70);
g.drawString("Font Style: " + fontStyle, 10, 90);
}
}
Example Output

Bookmark/Search this post with: