Introduction to Java Font
Java Font is a category that represents a font utilized in Graphical Person Interfaces (GUIs) within the Java programming language. It defines how textual content will probably be displayed on a GUI part, together with its font, dimension, and magnificence. The font class has various constructors that permit builders create a number of fonts with varied attributes. The syntax for making a font object is
Font font = new Font(String title, int model, int dimension)
the place,
title is the font title
model is the model of the font (daring, italic, and many others.)
dimension is the font dimension in factors
Key Takeaways
Fonts are important to any graphical person interface, and Java offers 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 offers a platform-independent solution to measure.
Understanding the various kinds of fonts, comparable to serif, sans-serif, and monospace, will help builders select the appropriate font for his or her purposes.
What Are the Forms of Java Font?
The correct font utilization could make all of the distinction when creating Graphical Person Interfaces (GUIs) in Java programming. Its fonts are the cornerstone of any GUI’s textual components, together with textual content labels, buttons, and menus. Builders working with Java have entry to varied fonts, divided into two foremost 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. The entire system’s bodily fonts are accessible by way of the Java platform. The Font.createFont() technique can be utilized to load the precise fonts into this system. The above technique takes a font file as enter and produces a font object the appliance can use.
These fonts remodel character sequences into glyph sequences utilizing font applied sciences like TrueType Fonts (TTF) and PostScript Sort 1 Font. All Java implementations should help TTF. Only some writing techniques, comparable to Latin, Japanese, and Primary Latin, are supported by bodily fonts.
2. Logical Fonts
All techniques 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, relatively than precise font recordsdata. 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 title 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 elements of the Java Font class. The font title is a string used to determine the font, and the household title is a common title used to determine a gaggle of font faces. A font face is a typeface used for rendering a particular font.
Logical Font Identify
Serif, SansSerif, Monospaced, Dialogue, and DialogInput are the 5 font households the Java platform provides. A logical font title is a reputation that’s appropriate with any machine. The Java runtime atmosphere 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 title is “Serif,” which maps to a bodily font on the system.
Font Face Identify
A person font model inside a font household is referred to by its font face title. 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 could solely generally be out there.
Instance
Font font = new Font(“Serif”, Font.BOLD, 12);
The font face title, on this occasion, “Daring,” designates that the font must be displayed in daring model.
Household Identify
A household title 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 usually linked to a particular font model or style.
Instance
Font font = new Font(“Serif”, Font.BOLD, 12);
The household title 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 accommodates many fields that permit builders alter the font, dimension, and magnificence of textual content that the GUI part shows. The fields out there within the Font class are defined in nice element under.
1. Static int BOLD
The BOLD area is a static int that represents the daring font model. The textual content leads to daring when this area is used to create a font object.
2. Static int CENTER_BASELINE
The CENTER_BASELINE area 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 area is a static string fixed representing the “Dialog” logical font’s canonical household title. It creates a Font object with the “Dialog” font household.
4. Static string fixed DIALOG_INPUT
The DIALOG_INPUT area is a static string fixed representing the “DialogInput” logical font’s household title. It creates a Font object with the “DialogInput” font household.
5. Static int HANGING_BASELINE
The HANGING_BASELINE area is a static int denoting the start line for textual content format in Devanigiri and associated scripts. It specifies the baseline of characters that cling under the road, comparable to letters with descenders.
6. Static int ITALIC
The ITALIC area is a static int that represents the italic font model. This area creates a font object, and the textual content leads to an italicized model.
7. Static int LAYOUT_LEFT_TO_RIGHT
The LAYOUT_LEFT_TO_RIGHT area is a static int that’s used to point to layoutGlyphVector that, in accordance with Bidi evaluation, the textual content is left to proper. It’s used to present the textual content route in a textual content format.
8. Static int LAYOUT_NO_LIMIT_CONTEXT
The LAYOUT_NO_LIMIT_CONTEXT area is a static int used as a flag for layoutGlyphVector. It signifies that textual content within the char array that extends previous a particular restrict shouldn’t be examined. It’s utilized to restrict the scope of textual content that’s examined in a textual content format.
9. Static int LAYOUT_NO_START_CONTEXT
This area is used as a flag within the layoutGlyphVector technique to point that the developer mustn’t take a look at the textual content within the char array first earlier than the required begin. This may be helpful when laying out textual content with a considerable amount of further formatting or metadata in the beginning of the textual content block that isn’t related to the format.
10. Static int LAYOUT_RIGHT_TO_LEFT
In keeping with Bidi evaluation, this area is used as a flag within the layoutGlyphVector technique 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 area represents the logical typeface “Monospaced” and has a String fixed representing its canonical household title. A monospaced font is one the place each character has the identical width, which will help show code or different textual content varieties the place character alignment is crucial.
12. Static string Identify
This area is a operate that returns the logical title of the font. The logical title of the font is the title that identifies it in a font database and could also be totally different from the font’s show title.
13. Static int PLAIN
This area represents a easy, timeless design of the font. It’s the default model for fonts and can be utilized for a lot of purposes.
14. Static float pointSize
This area 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 screen or printed on a web page.
15. Static int ROMAN_BASELINE
This area represents the most typical foundation line for textual content format 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 area represents the logical font “SansSerif” and has a String fixed representing its canonical household title. 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 area represents the logical typeface “Serif” and has a String fixed representing its canonical household title. 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 area represents the font’s level dimension and is expressed as an integer. It’s protected, which means that subclasses of the Font class can solely entry it.
19. Protected int Fashion
This area represents the font model provided to the operate Object(). It’s protected, which means that subclasses of the Font class can solely entry it.
20. Static int TRUETYPE_FONT
This area is a continuing to determine a TrueType font useful resource. TrueType is a well-liked digital font know-how developed by Apple Laptop and broadly adopted for fonts on Home windows and Macintosh computer systems.
21. Static int TYPE1_FONT
This area is a continuing that can be utilized to discover a font useful resource with the Sort 1 font household. Sort 1 fonts are a format for digital fonts developed by Adobe Methods 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 whenever you wish 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 title “Arial“, a plain model, 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 accommodates font attributes as key-value pairs. It creates a brand new Font object with the required 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 accommodates 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 required properties.
#3 Font(String title, int model, int dimension)
The third constructor of the Font class takes three arguments: font title, font model, and font dimension. It creates a brand new Font object with the required attributes.
Instance
// Creating a brand new font object utilizing font title, model, 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 title “Courier New“, a daring and italic model, and a dimension of 18 factors.
Output
All three constructors of the Font class create a brand new Font object with the required attributes. The ensuing font object is helpful for setting the GUI part’s font.
Strategies of Font Class
1. canDisplay(char c)
This technique determines whether or not the required Font accommodates a glyph for the requested character.
2. canDisplay(int codePoint)
This technique determines whether or not the required Font accommodates a glyph for the requested Unicode code level.
3. canDisplayUpTo(char[] textual content, int begin, int restrict)
This technique signifies whether or not the supplied textual content could be displayed utilizing this font, beginning initially index and ending on the restrict index.
4. canDisplayUpTo(CharacterIterator iter, int begin, int restrict)
This technique signifies whether or not the textual content outlined by the CharacterIterator iter, beginning initially index and ending on the restrict index, could be displayed utilizing this font.
5. canDisplayUpTo(String str)
This technique signifies whether or not the supplied string could be displayed utilizing this font.
6. createFont(int fontFormat, File fontFile)
This technique creates a brand new Font that returns utilizing the supplied font kind and specified font file.
7. createFont(int fontFormat, InputStream fontStream)
This technique creates a brand new Font that returns utilizing the supplied font kind and specified font stream.
8. createGlyphVector(FontRenderContext frc, char[] chars)
This technique 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 technique 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 technique converts the characters within the specified string to glyphs utilizing this font’s Unicode cmap to construct a GlyphVector.
11. deriveFont(float dimension)
This technique creates a brand new font object with the identical font face, model, and remodel as this font however with a brand new level dimension specified by the scale parameter.
12. getFamily()
This technique will get the household title of this font.
13. getSize()
This technique will get the purpose dimension of this font.
14. getStyle()
This technique will get the model of this font.
15. getName()
This technique will get the logical title of this font.
16. getItalicAngle()
This technique returns the angle in radians that the required Font is italicized.
17. getNumGlyphs()
This technique returns the variety of glyphs on this Font.
18. getPSName()
This technique returns the PostScript title of this Font.
19. getTransform()
This technique returns the AffineTransform related to this Font.
20. getAttributes()
This technique returns an immutable Map of all of the attributes of this Font.
21. getFontName()
This technique returns the title of this font.
22. isBold()
This technique returns true if this font has a daring model and false in any other case.
23. isItalic()
This technique returns true if this font has an italic model and false in any other case.
24. isPlain()
This technique returns true if this font has a plain model (i.e., neither daring nor italic) and false in any other case.
25. public Font deriveFont(Map<TextAttribute, ?> attributes)
This technique returns a brand new Font object with the required attributes.
26. public boolean canDisplay(char c)
This technique returns true if this Font can show the required character and false in any other case.
27. public boolean canDisplay(int codePoint)
This technique returns true if this Font can show the required Unicode code level and false in any other case.
28. deriveFont(Map<? extends AttributedCharacterIterator.Attribute, ?> attributes)
This technique creates a brand new Font object primarily based on this Font with the required attributes.
29. deriveFont(int model)
This technique creates a brand new Font object primarily based on this Font with the required model.
30. deriveFont(int model, float dimension)
This technique creates a brand new Font object primarily based on this Font with the required model and dimension.
31. equals(Object obj)
This technique compares this Font object to the required Object.
32. hasLayoutAttributes()
This technique returns a boolean worth denoting whether or not or not this Font has any format attributes.
33. hashCode()
This technique provides a hash code worth for the given Font object.
34. toString()
This technique provides 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 foremost(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(“Howdy, World!”, 50, 100);
}
};
body.add(panel);
body.setVisible(true);
}
}
Output
Instance 2
import java.awt.Font;
public class FontExample {
public static void foremost(String[] args) {
Font font = new Font(“Arial”, Font.BOLD, 18);
System.out.println(“Font title: ” + font.getFontName());
System.out.println(“Font model: ” + 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 coloration
g2.setColor(Coloration.RED);
// Draw textual content
g2.drawString(“Howdy World!”, 50, 100);
}
public static void foremost(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 facet of Java programming, permitting builders to customise the appear and feel of their purposes. The Java Font class offers a variety of strategies for builders to work with, permitting them to create, manipulate, and customise font objects of their Java packages. With Java Fonts, builders can create visually interesting and user-friendly purposes that stand out from the competitors!
Really helpful Articles
We hope that this EDUCBA info on “Java Font” was helpful to you. You possibly can view EDUCBA’s beneficial articles for extra info.
Java NIO FileChannel
What’s JavaScript?
Swap Assertion in JavaScript
Java Programming Fundamentals
The put up Java Font appeared first on EDUCBA.