×

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

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.