Class Font in Java:

Font class represents the various styles for the textual representation along with size.If we use the same character in a different font, they will appear differently. java supports five basic fonts.The font class is present in java.awt package.

Font class represents a font in a platform-independent way. The constructor accepts a font name, style, and point size.

Basic fonts in java:

  1. TimesRoman
  2. Helvetica
  3. Courier
  4. Dialog
  5. DialogInput

The default font is TimesRoman for applications and applets.

Three characters of the font:

  1. font name
  2. style
  3. size

The generic styles of a font:

  1. serif
  2. Sans-serif
  3. Monospaced
  4. Dialog
  5. DialogInput

The actual styles of a font:

  • Arial Black
  • Helvetica
  • Courier New

The style of a font :

It is specified by named constants defined in Font class. They are as follows:

  1. Font.PLAIN
  2. Font.ITALIC
  3. Font.BOLD
  4. Font.BOLD+Font.Italic

The size of the font is an integer ranging from 10 to 36 points but we can use a lower number or higher number based on our need. The size of a font is equal to the height of the largest character in the font in terms of the pixel. The default font size is 12.

The class method getFont() looks up the specified name in the system properties list and returns the font specified as the value of that property. It can also take an optional Font default to use if the named font property not found. However, it allows user customizability.

In short, the Font class represents a specific font to the system.

The class Font is defined as:

public class java.awt.Font extends java.lang.Object implements java.io.Serializable {
//member elements
protected String name;//the logical name of the font
protected int size;//the point size of the font
protected int style;//the style of the font.This is the sum of 
//constants PLAIN,BOLD,ITALIC such as:
public final static int BOLD;
public final static int ITALIC;
public final static int PLAIN;
//constructors:
public Font(String name,int style,int size);//creates a new Font with the specified name style and point size.
//class methods
public static Font decode(String str);
public static Font getFont(String str);
public static Font getFont(String str,Font font);
//methods
public boolean equals(Object obj);
public boolean isBold();
public boolean isItalic();
public boolean isPlain();
public String toString();
public String getName();
public String getFamily();
public int hashCode();
public int getStyle();
public int getSize();
public FontPeer getPeer();
public static Font getFont(String mn);
public static Font getFont(String mn,Font font);
public void setFont(Font f);
//Font f is represented as new Font("Helvetica",Font.Plain,12);
//Other styles are:
//Font.Plain,Font.BOLD,Font.Italic or Font.Italic+Font.Bold
}

The details of the class structure are given as follows:

public static final int BOLD;

public static final int BOLD represents a constant for specifying bold fonts.

public static final int ITALIC;

public static final int ITALIC represents a constant for specifying fonts.

public static final int PLAIN;

public static final int PLAIN represents a constant for specifying plain fonts.

protected String name;

protected String name represents the font’s logical name.

protected int size;

protected int size represents a the font size; allegedly in points, though probably not true typographer’s points.

protected int style;

protected int style represents a the font style, e.g., bold or italic or a combination thereof.

public Font (String name, int style, int size);

public Font (String name, int style, int size) constructs a Font object with the given characteristics.

Parameter
name – The name of the desired font.
style – One of the style flags (PLAIN, BOLD, or ITALIC) or a combination.
size – The size of the font to create.

public static Font decode (String str);

public static Font decode (String str) method gets font specified by str.

This method returns the Font instance requested, or default if str is invalid.

Parameter
str – The string describing the font.

public static Font getFont (String name)

public static Font getFont (String name) method gets font specified by the system property name.

This method returns the Font instance for name requested, or null if the name is invalid.

Parameter
name – The name of a system property specifying a font to fetch.

public static Font getFont (String name, Font defaultFont)

public static Font getFont (String name, Font defaultFont) method gets font specified by the system property name.

This method returns the Font instance of name requested, or defaultFont if the name is invalid.

Parameter
name – The name of a system property specifying a font to fetch.
defaultFont – Font to return if name not found in properties.

public boolean equals (Object object);

public boolean equals (Object object) method compares two different Font instances for equivalence.

This method returns true if the objects are equivalent fonts (same name, style, and point size), false otherwise.

Parameter
object – The object to compare.

public String getFamily();

public String getFamily() method returns the actual name of the font.

This method returns the actual name of the font.

public String getName();

public String getName() method returns the logical name of the font.

This method returns the logical name of the font.

public FontPeer getPeer();

public FontPeer getPeer() method returns the font’s peer.

This method returns the font’s peer.

public int getSize();

public int getSize() method returns the size parameter from creation.

This method returns the size parameter from creation.

public int getStyle();

public int getStyle() method returns the style parameter from creation.

This method returns the style parameter from creation.

public int hashCode();

public int hashCode() method generates a hashcode for the Font.

This method returns a hashcode to use when using the Font as a key in a Hashtable.

public boolean isBold();

public boolean isBold() method returns true if Font style is bold, false otherwise.

This method returns true if the Font style is bold, false otherwise.

public boolean isItalic();

public boolean isItalic() method returns true if Font style is italic, false otherwise.

This method returns true if the Font style is italic, false otherwise.

public boolean isPlain();

public boolean isPlain() method returns true if Font style is neither bold nor italic, false otherwise.

This method returns true if Font style is neither bold nor italic, false otherwise.

public String toString();

public String toString() method returns a string representation of the Font object.
This method returns a string representation of the Font object.

How to create a font:

we can construct a new font by the following code:

Font myFont=new Font("Serif",Font.Plain,12); 

Every graphics context has a current font that helps us to write text. However, we can change it by using the setFont() method.so if g is the graphics context then we can use

 g.setFont(myFont); 

Every component has an associated font. It can also be changed with setFont() method defined in the Component class. When the graphics context is created for drawing on components, the graphics context’s current font is set to the font of the component.

Example of drawing using Font

public void myComponent(Graphics g){
Font f=new Font("Serif",Font.BOLD,12) g.setFont(f); g.drawString("Hello World",10,50); }

Java supports antialiasing to draw graphics along with fonts more smoothly. This also provides a less blocky appearance. To enable this we have enabled Graphics2D’s set rendering hint in the swing.

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,renderingHints.VALUE_ANTIALIAS_ON)

The arguments are

  • The key to the rendering hint
  • The value to associate with the key.

An example to display Font


package testPackage;

import java.awt.*;
public class MyDisplayFont extends Frame {
	static String[] MyFonts;
	private Dimension dim;
	MyDisplayFont () {
		super ("My Frame for Font Display");
		MyFonts = Toolkit.getDefaultToolkit().getFontList();
	}
	public void addNotify() {
		Font font;
		super.addNotify();
		int height = 0;
		int maxWidth = 0;
		final int varMargin = 5, heightMargin = 5;
		for (int i=0;iMath.max (maxWidth, getWidth (font, MyFonts[i] + " BOLDITALIC"));
		}
		Insets inset = insets();
		dim = new Dimension (maxWidth + inset.left + inset.right + heightMargin,


				height + inset.top + inset.bottom + varMargin);
		resize (dim);
	}
	static public void main (String args[]) {
		MyDisplayFont mdf = new MyDisplayFont();
		mdf.show();
	}
	private int getHeight (Font f) {
		FontMetrics Myfm = Toolkit.getDefaultToolkit().getFontMetrics(f);
		return Myfm.getHeight();
	}
	private int getWidth (Font f, String s) {
		FontMetrics Myfm = Toolkit.getDefaultToolkit().getFontMetrics(f);
		return Myfm.stringWidth(s);
	}
	public void paint (Graphics gra) {
		int x = 0;
		int y = 0;
		gra.translate(insets().left, insets().top);
		for (int i=0;i

The output of the code:

How to display font in java?
How to display font in java?

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment