Introduction to Java Font
Java Font is a category that represents a font utilized in Graphical Consumer Interfaces (GUIs) within the Java programming language. It defines how textual content will probably be displayed on a GUI element, together with its font, dimension, and elegance. The font class has numerous constructors that permit builders create a number of fonts with numerous attributes. The syntax for making a font object is
Font font = new Font(String identify, int fashion, int dimension)
the place,
- identify is the font identify
- fashion is the fashion of the font (daring, italic, and so on.)
- dimension is the font dimension in factors
Key Takeaways
- Fonts are important to any graphical person interface, and Java gives an in depth set of font-related lessons and strategies.
- The Font class in Java permits builders to create and manipulate font objects and set font properties.
- Java’s FontRenderContext class gives a platform-independent approach to measure.
- Understanding the various kinds of fonts, comparable to serif, sans-serif, and monospace, can assist builders select the correct font for his or her purposes.
What Are the Varieties of Java Font?
The suitable font utilization could make all of the distinction when creating Graphical Consumer Interfaces (GUIs) in Java programming. Its fonts are the cornerstone of any GUI’s textual parts, together with textual content labels, buttons, and menus. Builders working with Java have entry to varied fonts, divided into two major classes: bodily fonts and logical fonts.
1. Bodily Fonts
Bodily fonts are pre-installed on the pc’s working system and utilized by Java purposes. All the system’s bodily fonts are accessible by the Java platform. The Font.createFont() methodology can be utilized to load the precise fonts into this system. The above methodology takes a font file as enter and produces a font object the applying can use.
These fonts rework character sequences into glyph sequences utilizing font applied sciences like TrueType Fonts (TTF) and PostScript Kind 1 Font. All Java implementations should help TTF. Just a few writing programs, comparable to Latin, Japanese, and Fundamental Latin, are supported by bodily fonts.
2. Logical Fonts
All programs that help the Java platform will undoubtedly have entry to the logical fonts. The logical fonts are a set of 5 font households, every with distinctive qualities, quite than precise font information. Serif, SansSerif, Monospaced, Dialogue, and DialogInput are the 5 font households.
The logical font names are transformed to bodily fonts by the JRE. Every logical font identify maps to a number of bodily fonts to help a broad vary of characters.
What Are the Font Faces and Names?
Font faces and names are key parts of the Java Font class. The font identify is a string used to determine the font, and the household identify is a common identify used to determine a gaggle of font faces. A font face is a typeface used for rendering a selected font.
Logical Font Title
Serif, SansSerif, Monospaced, Dialogue, and DialogInput are the 5 font households the Java platform affords. A logical font identify is a reputation that’s appropriate with any system. The Java runtime setting maps the logical font names and the bodily put in fonts on the system.
Instance
Font font = new Font("Serif", Font.BOLD, 12);
On this instance, the logical font identify is “Serif,” which maps to a bodily font on the system.
Font Face Title
A person font fashion inside a font household is referred to by its font face identify. Every font household could include a number of font faces with totally different kinds, like daring or italic. Every font household has a novel set of font face names, and relying on the system, they might solely typically be accessible.
Instance
Font font = new Font("Serif", Font.BOLD, 12);
The font face identify, on this occasion, “Daring,” designates that the font needs to be displayed in daring fashion.
Household Title
A household identify is a collective time period used to explain a set of font faces. A font household contains a wide range of font faces which have the identical x-height and stroke width. Font faces are categorized utilizing household names, that are sometimes related to a selected font fashion or style.
Instance
Font font = new Font("Serif", Font.BOLD, 12);
The household identify used within the instance talked about above is “Serif,” which encompasses all serif font faces.
Fields of Font Class
Java’s Font class is an efficient software for representing and controlling fonts in graphical person interfaces. The Font class incorporates many fields that permit builders alter the font, dimension, and elegance of textual content that the GUI element shows. The fields accessible within the Font class are defined in nice element beneath.
1. Static int BOLD
The BOLD subject is a static int that represents the daring font fashion. The textual content leads to daring when this subject is used to create a font object.
2. Static int CENTER_BASELINE
The CENTER_BASELINE subject is a static int that represents the muse for arranging textual content in ideographic characters, together with Chinese language, Japanese, and Korean. It specifies the middle baseline of the textual content, which is used to align characters vertically.
3. Static string fixed DIALOG
The DIALOG subject is a static string fixed representing the “Dialog” logical font’s canonical household identify. It creates a Font object with the “Dialog” font household.
4. Static string fixed DIALOG_INPUT
The DIALOG_INPUT subject is a static string fixed representing the “DialogInput” logical font’s household identify. It creates a Font object with the “DialogInput” font household.
5. Static int HANGING_BASELINE
The HANGING_BASELINE subject is a static int denoting the start line for textual content structure in Devanigiri and associated scripts. It specifies the baseline of characters that hold beneath the road, comparable to letters with descenders.
6. Static int ITALIC
The ITALIC subject is a static int that represents the italic font fashion. This subject creates a font object, and the textual content leads to an italicized fashion.
7. Static int LAYOUT_LEFT_TO_RIGHT
The LAYOUT_LEFT_TO_RIGHT subject is a static int that’s used to point to layoutGlyphVector that, in response to Bidi evaluation, the textual content is left to proper. It’s used to present the textual content path in a textual content structure.
8. Static int LAYOUT_NO_LIMIT_CONTEXT
The LAYOUT_NO_LIMIT_CONTEXT subject is a static int used as a flag for layoutGlyphVector. It signifies that textual content within the char array that extends previous a selected restrict shouldn’t be examined. It’s utilized to restrict the scope of textual content that’s examined in a textual content structure.
9. Static int LAYOUT_NO_START_CONTEXT
This subject is used as a flag within the layoutGlyphVector methodology to point that the developer shouldn’t have a look at the textual content within the char array first earlier than the desired begin. This may be helpful when laying out textual content with a considerable amount of extra formatting or metadata initially of the textual content block that isn’t related to the structure.
10. Static int LAYOUT_RIGHT_TO_LEFT
In line with Bidi evaluation, this subject is used as a flag within the layoutGlyphVector methodology to point that the textual content is right-to-left. It may be helpful when laying out textual content in languages comparable to Arabic or Hebrew, learn from proper to left.
11. Static string MONOSPACED
This subject represents the logical typeface “Monospaced” and has a String fixed representing its canonical household identify. A monospaced font is one the place each character has the identical width, which can assist show code or different textual content sorts the place character alignment is crucial.
12. Static string Title
This subject is a perform that returns the logical identify of the font. The logical identify of the font is the identify that identifies it in a font database and could also be totally different from the font’s show identify.
13. Static int PLAIN
This subject represents a easy, timeless design of the font. It’s the default fashion for fonts and can be utilized for a lot of purposes.
14. Static float pointSize
This subject represents the font’s level dimension and is expressed as a float. The purpose dimension of a font is nothing however the dimension at which it’s designed to be displayed on a given display or printed on a web page.
15. Static int ROMAN_BASELINE
This subject represents the commonest foundation line for textual content structure in Roman scripts. It’s used to find out the baseline of a font when it’s being laid out on a web page.
16. Static string SANS_SERIF
This subject represents the logical font “SansSerif” and has a String fixed representing its canonical household identify. A sans-serif font doesn’t have small traces or serifs on the finish of the strokes of the letters, and it’s typically used for contemporary and clear designs.
17. Static string SERIF
This subject represents the logical typeface “Serif” and has a String fixed representing its canonical household identify. A serif font has small traces or serifs on the finish of the letters’ strokes, and it’s typically used for extra conventional designs.
18. Protected int Measurement
This subject represents the font’s level dimension and is expressed as an integer. It’s protected, that means that subclasses of the Font class can solely entry it.
19. Protected int Fashion
This subject represents the font fashion provided to the perform Object(). It’s protected, that means that subclasses of the Font class can solely entry it.
20. Static int TRUETYPE_FONT
This subject is a continuing to determine a TrueType font useful resource. TrueType is a well-liked digital font expertise developed by Apple Laptop and broadly adopted for fonts on Home windows and Macintosh computer systems.
21. Static int TYPE1_FONT
This subject is a continuing that can be utilized to discover a font useful resource with the Kind 1 font household. Kind 1 fonts are a format for digital fonts developed by Adobe Programs and are generally utilized in skilled graphic design and printing.
Constructors of Font Class
#1 Font(Font font)
The primary constructor of the Font class takes an present Font object as an argument and creates a brand new Font object with the identical properties. That is useful while you need to develop a brand new Font object with the identical attributes as an present Font object.
Instance
// Creating a brand new font object
Font font1 = new Font("Arial", Font.PLAIN, 12);
// Utilizing the prevailing font object to create a brand new font object
Font font2 = new Font(font1);
On this instance, we create a brand new Font object named “font1” with the identify “Arial“, a plain fashion, and a dimension of 12 factors. Then, we use the prevailing “font1” object to create a brand new Font object named “font2” with the identical attributes.
#2 Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
The second constructor of the Font class takes a Map object that incorporates font attributes as key-value pairs. It creates a brand new Font object with the desired properties.
Instance
// Making a Map object with font attributes
Map<AttributedCharacterIterator.Attribute, Object> fontAttributes = new HashMap<>();
fontAttributes.put(TextAttribute.FAMILY, "Instances New Roman");
fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
fontAttributes.put(TextAttribute.SIZE, 16);
// Creating a brand new font object utilizing the map of attributes
Font font3 = new Font(fontAttributes);
On this instance, we create a Map object named “fontAttributes” that incorporates font attributes comparable to household, weight, and dimension. Then, we use this Map object to create a brand new Font object named “font3” with the desired properties.
#3 Font(String identify, int fashion, int dimension)
The third constructor of the Font class takes three arguments: font identify, font fashion, and font dimension. It creates a brand new Font object with the desired attributes.
Instance
// Creating a brand new font object utilizing font identify, fashion, and dimension
Font font4 = new Font("Courier New", Font.BOLD | Font.ITALIC, 18);
On this instance, we create a brand new Font object named “font4” with the identify “Courier New“, a daring and italic fashion, and a dimension of 18 factors.
Output
All three constructors of the Font class create a brand new Font object with the desired attributes. The ensuing font object is beneficial for setting the GUI element’s font.
Strategies of Font Class
1. canDisplay(char c)
This methodology determines whether or not the desired Font incorporates a glyph for the requested character.
2. canDisplay(int codePoint)
This methodology determines whether or not the desired Font incorporates a glyph for the requested Unicode code level.
3. canDisplayUpTo(char[] textual content, int begin, int restrict)
This methodology signifies whether or not the supplied textual content could be displayed utilizing this font, beginning firstly index and ending on the restrict index.
4. canDisplayUpTo(CharacterIterator iter, int begin, int restrict)
This methodology signifies whether or not the textual content outlined by the CharacterIterator iter, beginning firstly index and ending on the restrict index, could be displayed utilizing this font.
5. canDisplayUpTo(String str)
This methodology signifies whether or not the supplied string could be displayed utilizing this font.
6. createFont(int fontFormat, File fontFile)
This methodology creates a brand new Font that returns utilizing the supplied font sort and specified font file.
7. createFont(int fontFormat, InputStream fontStream)
This methodology creates a brand new Font that returns utilizing the supplied font sort and specified font stream.
8. createGlyphVector(FontRenderContext frc, char[] chars)
This methodology converts the characters within the array to glyphs utilizing this font’s Unicode cmap to construct a GlyphVector.
9. createGlyphVector(FontRenderContext frc, CharacterIterator ci)
This methodology converts the characters within the CharacterIterator to glyphs utilizing this font’s Unicode cmap to construct a GlyphVector.
10. createGlyphVector(FontRenderContext frc, String str)
This methodology converts the characters within the specified string to glyphs utilizing this font’s Unicode cmap to construct a GlyphVector.
11. deriveFont(float dimension)
This methodology creates a brand new font object with the identical font face, fashion, and rework as this font however with a brand new level dimension specified by the dimensions parameter.
12. getFamily()
This methodology will get the household identify of this font.
13. getSize()
This methodology will get the purpose dimension of this font.
14. getStyle()
This methodology will get the fashion of this font.
15. getName()
This methodology will get the logical identify of this font.
16. getItalicAngle()
This methodology returns the angle in radians that the desired Font is italicized.
17. getNumGlyphs()
This methodology returns the variety of glyphs on this Font.
18. getPSName()
This methodology returns the PostScript identify of this Font.
19. getTransform()
This methodology returns the AffineTransform related to this Font.
20. getAttributes()
This methodology returns an immutable Map of all of the attributes of this Font.
21. getFontName()
This methodology returns the identify of this font.
22. isBold()
This methodology returns true if this font has a daring fashion and false in any other case.
23. isItalic()
This methodology returns true if this font has an italic fashion and false in any other case.
24. isPlain()
This methodology returns true if this font has a plain fashion (i.e., neither daring nor italic) and false in any other case.
25. public Font deriveFont(Map<TextAttribute, ?> attributes)
This methodology returns a brand new Font object with the desired attributes.
26. public boolean canDisplay(char c)
This methodology returns true if this Font can show the desired character and false in any other case.
27. public boolean canDisplay(int codePoint)
This methodology returns true if this Font can show the desired Unicode code level and false in any other case.
28. deriveFont(Map<? extends AttributedCharacterIterator.Attribute, ?> attributes)
This methodology creates a brand new Font object primarily based on this Font with the desired attributes.
29. deriveFont(int fashion)
This methodology creates a brand new Font object primarily based on this Font with the desired fashion.
30. deriveFont(int fashion, float dimension)
This methodology creates a brand new Font object primarily based on this Font with the desired fashion and dimension.
31. equals(Object obj)
This methodology compares this Font object to the desired Object.
32. hasLayoutAttributes()
This methodology returns a boolean worth denoting whether or not or not this Font has any structure attributes.
33. hashCode()
This methodology offers a hash code worth for the given Font object.
34. toString()
This methodology offers a String illustration of this Font object.
Examples of Java Font
Instance 1
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FontExample {
public static void major(String[] args) {
JFrame body = new JFrame();
body.setSize(300, 300);
body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Verdana", Font.BOLD, 40);
g2d.setFont(font);
g2d.drawString("Hi there, World!", 50, 100);
}
};
body.add(panel);
body.setVisible(true);
}
}
Output
Instance 2
import java.awt.Font;
public class FontExample {
public static void major(String[] args) {
Font font = new Font("Arial", Font.BOLD, 18);
System.out.println("Font identify: " + font.getFontName());
System.out.println("Font fashion: " + font.getStyle());
System.out.println("Font dimension: " + font.getSize());
}
}
Output
Instance 3
import java.awt.*;
import javax.swing.*;
public class FontExample2 extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Set font and dimension
Font font = new Font("Arial", Font.BOLD, 36);
g2.setFont(font);
// Set shade
g2.setColor(Shade.RED);
// Draw textual content
g2.drawString("Hi there World!", 50, 100);
}
public static void major(String[] args) {
JFrame body = new JFrame("Font Instance");
body.getContentPane().add(new FontExample2());
body.setSize(300, 200);
body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
body.setVisible(true);
}
}
Output
Conclusion
In conclusion, Java Fonts are a necessary side of Java programming, permitting builders to customise the feel and appear of their purposes. The Java Font class gives a variety of strategies for builders to work with, permitting them to create, manipulate, and customise font objects of their Java applications. With Java Fonts, builders can create visually interesting and user-friendly purposes that stand out from the competitors!
Beneficial Articles
We hope that this EDUCBA data on “Java Font” was useful to you. You’ll be able to view EDUCBA’s advisable articles for extra data.