Java Font Example

Java Font Example

Java AWT supports graphics drawing in colors and fonts. To support colors and fonts, the Java.awt package come with two classes – class Color and class Font.
Java Font Example to create font objects and changing change string fonts.

Java Font ExampleOutput Screenshot on Java Font Example

Font f1 = new Font(“Monospaced”, Font.BOLD, 12);
Font f2 = new Font(“Dialog”, Font.BOLD+Font.ITALIC, 15);

Two Font objects are created. Font.BOLD+Font.ITALIC gives both and bold and italic effect. Java supports 5 types of font names – Monospaced , Dialog, DialogInput, Serif and SansSerif. To give font styles three variables are given in Font class – PLAIN, BOLD and ITALIC.

g.setFont(f1);

Using setFont() method of Graphics class, font is set to graphics drawn. When f2 is set, the earlier f1 gets invalidated implicitly.

g.drawString(“Hello World”, 50, 100);
g.drawString(“Best Wishes from way2java Readers”, 50, 125);

drawString() method of Graphics class writes a string on the frame. The parameters are the string to be drawn, x and y coordinates where string to be drawn. For more on drawing string in colors and fonts read Strings in Colors and Fonts.

Know also on Java Color Example.