×

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:

  1. Physical Fonts
  2. 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        FieldDescription
Static intBOLDAlways in a bold manner.  
Static intCENTER_BASELINEthe foundation used when arranging text in ideographic characters including Chinese, Japanese, and Korean.  
static stringDIALOGa String constant for "Dialog," the logical font's canonical family name.  
Static stringDIALOG_INPUTa String constant representing the logical font "DialogInputcanonical "'s family name.  
Static intHANGING_BASELINEthe starting point for text layout in Devanigiri and related scripts.
Static intITALICthe continual italicized style.  
Static intLAYOUT_LEFT_TO_RIGHTAn indication to layoutGlyphVector that, according to Bidi analysis, the text is left to right.  
Static intLAYOUT_NO_LIMIT_CONTEXTLayoutGlyphVector is given a flag indicating that text in the char array that extends past a certain limit shouldn't be examined.  
Static intLAYOUT_NO_START_CONTEXTa flag for layoutGlyphVector specifying the text in the char array first before the specified start shouldn't be looked at.  
Static intLAYOUT_RIGHT_TO_LEFTa flag for layoutGlyphVector that tells it the text is right-to-left according to Bidi analysis.  
Static stringMONOSPACEDThe logical typeface "Monospaced" has a String constant representing its canonical family name.  
Static stringNameThe function Object() { [native code] } was given this Font's logical name.  
Static intPLAINa simple, timeless design.  
Static floatpointSizeThis Font's point size is in floats.  
Static intROMAN_BASELINEmost common basis line for text layout in Roman scripts.  
Static stringSANS_SERIFThe logical font "SansSerif" has a String constant representing its canonical family name.  
Static stringSERIFThe logical typeface "Serif" has a String constant representing its canonical family name.  
protected intSizeThe font's point size, is expressed as an integer.  
protected intStyleThe font style that was supplied to the function Object() { [native code] }.  
Static intTRUETYPE_FONTIdentify a TRUETYPE font resource.  
Static intTYPE1_FONTFind 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:

Java Font
Java Font

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:

Java Font

Related Topics

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.