Class FontMetrics in Java

Class FontMetrics in Java
Class FontMetrics in Java

Class FontMetrics in Java:

This class represents a FontMetrics object,which gives information about the rendering of a particular font on the screen.When an application asks AWT to place a character on the screen at a particular position(x,y),the character is placed so that its reference point is put at that position(shown as a dot on the right part of the screen).

The reference point :

It specifies a horizontal line known as the baseline of a character.In normal painting,the baselines of the characters should align.In addition,every character in a font has an ascent or descent and an advanced width.

The ascent 

It is the amount by which the character ascends above the baseline.

The descent 

It is the amount by which the character descent below the baseline.

The advance width 

It indicates the position at which AWT should place the next character.If the current character is placed with the reference point x,y and the character's advance width is w, then the following character is placed with its reference point at x+w.The adavance width is often the same as the width of character's bounding box,but it is not mandatory.Slanted and italic fonts ofetn have characters whose top right corner extends slightly beyond the advance width.

An array of characters or a String can also have an ascent,a descent and an advance width.The ascent of the array is the maximum ascent of any character in the array.Similarly the descent is the maximum descent of any character in the array.The advance width is the sum of the advance widths of each of the characters in the array.The default implementation of these methods are insufficient , they are usually overriden with more efficient toolkit specific implementation.

The FontMetrics class contains methods to find out the size of the current font or the width of a String drawn in that font. It is not related to contained in the Font class itself. but is dependent on where we are drawing the font. The graphics class contains the call getFontMetrics() which returns an instance of the class for that Graphics object.For this,we can only determine the actual size of a font with a paint or related method,where the current graphics context is available. 

The class structure of the FontMetrics is given as: 

public abstract class java.awt.FontMetrics extends java.lang.Object{
//Member element:
protected Font font;//the default font or actual font
protected FontMetrics(Font font);
//creates a new FontMetrics object for finding the height and width information about the 
//the specified font and specific character glyph in the font.
public int bytesWidth(byte data[],int off,int len);
public int charsWidth(chars data[],int off,int len);
public int charWidth(char ch);
public int charWidth(int ch);
public int getAscent();
public int getDescent();
public int getHeight();
public int getLeading();
public int getMaxAdvance();
public int getMaxAscent();
public int getMaxDescent();
public int[] getWidths();
public int stringWidth(String str);
public String toString();
public Font getFont();
Class FontMetrics in Java Class FontMetrics in Java Reviewed by Animesh Chatterjee on December 31, 2018 Rating: 5

No comments:

Powered by Blogger.