Java Font
The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is FontUIResource.
The font that is used to produce the text is represented by it. Characters and Glyphs are the two technical words used in Java to describe font.
Font types in Java
Java supports two different font types:
- Physical Fonts
- Logical fonts
Physical Fonts
Actual Java font libraries are used as physical fonts. It includes tables that use font technologies like TrueType Fonts (TTF) and PostScript Type 1 Font to convert character sequences to glyph sequences. Remember that TTF must be supported by all Java implementations. Using different font technologies depends on how they are implemented. Names of actual fonts like Helvetica, Palatino, HonMincho, and others are included.
The physical font's unique feature is that it only supports a small number of writing systems, such as Latin or Japanese and Basic Latin. Regarding configuration modifications, it could differ. The createFont() function of the Java Font class can be used to bundle and instantiate a specific font if an application needs it.
Logical Fonts
Serif, SansSerif, Monospaced, Dialog, and DialogInput are the five logical font families that Java defines. The JRE must be able to support it. Since these aren't the actual font libraries, it should be noted that JRE transfers the logical font names to physical fonts. Implementation of mapping is typically location specific. In order to accommodate a broad range of characters, each logical font name maps to multiple physical fonts.
For instance, AWT components like Label and TextField only employ logical typefaces.
Font Faces and Names
A typeface can come in a variety of styles, including heavy, normal, medium, oblique, gothic, etc. The typographic design is the same for all font faces.
A font object has three names, which are:
Logical font name: The name that is utilized to create the font is logically logical.
Font Face Name: This is the font face's name. Helvetica Bold, for instance.
Family Name: Among many faces, it chooses the typographic design.
A Java Font class represents a specific font face among a group of font faces that are available in the host system's system resources. Arial Bold and Courier Bold Italic are a couple of examples of font faces. A font face can be associated with many Font objects, each of which has different size, style, transform, and font features.
Font Class Fields
Modifier | Field | Description |
Static int | BOLD | Always in a bold manner. |
Static int | CENTER_BASELINE | the foundation used when arranging text in ideographic characters including Chinese, Japanese, and Korean. |
static string | DIALOG | a String constant for "Dialog," the logical font's canonical family name. |
Static string | DIALOG_INPUT | a String constant representing the logical font "DialogInputcanonical "'s family name. |
Static int | HANGING_BASELINE | the starting point for text layout in Devanigiri and related scripts. |
Static int | ITALIC | the continual italicized style. |
Static int | LAYOUT_LEFT_TO_RIGHT | An indication to layoutGlyphVector that, according to Bidi analysis, the text is left to right. |
Static int | LAYOUT_NO_LIMIT_CONTEXT | LayoutGlyphVector is given a flag indicating that text in the char array that extends past a certain limit shouldn't be examined. |
Static int | LAYOUT_NO_START_CONTEXT | a flag for layoutGlyphVector specifying the text in the char array first before the specified start shouldn't be looked at. |
Static int | LAYOUT_RIGHT_TO_LEFT | a flag for layoutGlyphVector that tells it the text is right-to-left according to Bidi analysis. |
Static string | MONOSPACED | The logical typeface "Monospaced" has a String constant representing its canonical family name. |
Static string | Name | The function Object() { [native code] } was given this Font's logical name. |
Static int | PLAIN | a simple, timeless design. |
Static float | pointSize | This Font's point size is in floats. |
Static int | ROMAN_BASELINE | most common basis line for text layout in Roman scripts. |
Static string | SANS_SERIF | The logical font "SansSerif" has a String constant representing its canonical family name. |
Static string | SERIF | The logical typeface "Serif" has a String constant representing its canonical family name. |
protected int | Size | The font's point size, is expressed as an integer. |
protected int | Style | The font style that was supplied to the function Object() { [native code] }. |
Static int | TRUETYPE_FONT | Identify a TRUETYPE font resource. |
Static int | TYPE1_FONT | Find a font resource with the TYPE1 font family. |
Font Class Constructors
Constructor | Description |
Font(Font font) | uses the provided font to create a new Font. |
Font(Map<?extends AttributedCharacterIterator.Attribute,?> attributes) | a replacement Font with the supplied properties is created. |
Font(String name, int style, int size) | uses the supplied name, style, and point size to create a new Font. |
Font Class Methods
Methods | Description |
canDisplay(char c) | It determines whether this Font contains a glyph again for the requested character. |
canDisplay(int codePoint) | It determines whether this Font contains a glyph again for the requested character. |
canDisplayUpTo(char[] text, int start, int limit) | It indicates if or not the provided text, starting at the start as well as ending at the limit, can be displayed using this font. |
canDisplayUpTo(CharacterIterator iter, int start, int limit) | It shows whether or not the text defined either by iter starting at the start as well as ending at the limit can be displayed using this font. |
canDisplayUpTo(String str) | It states whether or not the provided String can be displayed by this Font. |
createFont(int fontFormat, File fontFile) | The provided font type and specified font file are used to create a new Font that is returned. |
createFont(int fontFormat, InputStream fontStream) | The provided font type and specified font file are used to create a new Font that is returned. |
createGlyphVector(FontRenderContext frc, char[] chars) | Characters are converted into glyphs one-to-one using this font's Unicode cmap to build a GlyphVector. |
createGlyphVector(FontRenderContext frc, CharacterIterator ci) | The provided characters are converted one-to-one to glyphs using this font's Unicode cmap to build a GlyphVector. |
createGlyphVector(FontRenderContext frc, int[] glyphCodes) | Characters are converted into glyphs one-to-one using this font's Unicode cmap to build a GlyphVector. |
createGlyphVector(FontRenderContext frc, String str) | Characters are converted into glyphs one-to-one using this font's Unicode cmap to build a GlyphVector. |
decode(String str) | The Font that the str parameter describes is returned. |
deriveFont(AffineTransform trans) | By duplicating the current Font object and giving it a fresh transform, it produces a new Font object. |
deriveFont(float size) | By copying the current Font object and giving it a new size, it generates a new Font object. |
deriveFont(int style) | By copying the current Font object and giving it a new size, it generates a new Font object. |
deriveFont(int style, AffineTransform trans) | By duplicating this Font object and adding a new style and transform, it produces a new Font object. |
deriveFont(int style, float size) | This Font object is duplicated and given a new style and size to produce a new Font object. |
deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes) | The current Font object is duplicated and given a fresh set of font properties to produce a new Font object. |
equals(Object obj) | It evaluates this Font object against the designated Object. |
getAttributes() | A map of the font properties found in this Font is returned. |
getAvailableAttributes() | All of the attributes supported by this Font's keys are returned. |
getBaselineFor(char c) | It returns the starting point that is suitable for showing this character. |
getFamily() | It gives back the name of this Font's family. |
getFamily(Locale l) | The family name of this Font, localized for the requested locale, is returned. |
getFont(Map<?extends AttributedCharacterIterator.Attribute,?> attributes) | It provides a Font that is suitable for the properties. |
getFont(String nm) | A Font object is returned from the list of system properties. |
getFont(String nm, Font font) | Obtains the desired Font from the list of system attributes. |
getFontName() | It gives back the name of this Font's font face. |
getFontName(Locale l) | It gives the locale-specific font face name for the provided Font. |
getItalicAngle() | It gives this Font's italic angle back. |
getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc) | A LineMetrics object made with the supplied inputs is returned. |
getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc) | A LineMetrics object made with the supplied inputs is returned. |
getLineMetrics(String str, FontRenderContext frc) | It gives back a LineMetrics object that was made using the supplied FontRenderContext and String. |
getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc) | A LineMetrics object made with the supplied inputs is returned. |
getMaxCharBounds(FontRenderContext frc) | It gives back the character's boundaries with the highest bounds possible according to the chosen FontRenderContext. |
getMissingGlyphCode() | In cases when this Font lacks a glyph for a certain unicode code point, it returns the glyphCode. |
getName() | The logical identity of this Font is returned. |
getNumGlyphs() | The quantity of glyphs in this Font is returned. |
getPeer() | Now, font rendering works across all platforms. |
getPSName() | It gives back this Font's postscript name. |
getSize() | It gives back the font's point size as an integer that has been rounded up. |
getSize2D() | It gives back the font's point size as a float value. |
getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc) | It gives back the character array's logical boundaries in the chosen FontRenderContext. |
getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc) | It provides the provided CharacterIterator's logical boundaries for the characters that are indexed in the supplied FontRenderContext. |
getStringBounds(String str, FontRenderContext frc) | It gives back the String's logical boundaries in the supplied FontRenderContext. |
getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc) | It gives back the String's logical boundaries in the supplied FontRenderContext. |
getStyle() | It gives back this Font's look. |
getTransform() | It gives back a duplicate of the transform connected to this Font. |
hashCode() | For this Font, a hashcode is returned. |
hasLayoutAttributes() | If this Font has attributes that call for additional layout processing, it returns true. |
hasUniformLineMetrics() | It examines the uniformity of the font's line metrics. |
isBold() | It displays if the font object's style is BOLD or not. |
isItalic() | It shows whether the font object's style is ITALIC or not. |
isPlain() | It shows whether the style of this Font object is PLAIN or not. |
isTransformed() | It shows if the size of this Font object is affected by a transform in addition to the Size attribute. |
layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) | If possible, the text's complete layout is performed when a new GlyphVector object is returned. |
toString() | This Font object is transformed into a String representation. |
Let's look at a Java font sample.
JFE.java
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
public class JFE extends Applet
{
public void paint(Graphics gph)
{
// establishing a function Object() for the font class and providing the font's name, style, and size
// These three parameters can be adjusted appropriately.
Font font= new Font("Courier", Font.PLAIN, 20);
// Font is changed by calling the setFont() function.
gph.setFont(font);
gph.drawString(" The best leaning platform is JavaTpoint ", 11, 44);
}
}
/*
<applet code="JFE" width=300 height=300>
</applet>
*/
Output 1:


Make the necessary adjustments to the following statement to obtain the desired result:
Font font= new Font("Monospaced", Font.ITALIC, 30); // Name, font face, and font size are modified
Output 2:
