Java Font constructor

Java Font constructor: Font class sets font to the text. The text may represent a string displayed by drawString(), setText(), setLabel(), appendText() etc. methods.
Syntax of Font constructor to create a Font object

Font f1 = new Font(String fontName, int fontStyle, int fontSize);

Java Font constructor: To give the font style as an integer value, the Font class comes with three symbolic constants as follows.

public final static int PLAIN = 0;
public final static int BOLD = 1;
public final static int ITALIC = 2;

Java supports 5 font names. They are

Monospaced Serif Dialog
SansSerif DialogInput

If any other font name is given, it is not an error or exception, but takes Dialog as the default (style: plain and size: 12).


Font f1 = new Font("Monospaced", Font.BOLD, 15); // 1st style of Java Font constructor
Font f2 = new Font("SansSerif", Font.ITALIC, 18); // 2nd style of Java Font constructor
Font f3 = new Font("Dialog", Font.BOLD + Font.ITALIC, 20); // 3rd style of Java Font constructor

The above style of creating colors and fonts is used in all the graphics programs.


All AWT Components – At a Glance

Drawing All Java Geometrical Figures.

Pass your comments and suggestions to improve the quality of this tutorial "Java Font constructor".

Leave a Comment

Your email address will not be published.