Simple Font Paint Example

This section illustrates you how to paint the font.
A font is a specific style of displaying characters. It describes the design,
size, appearance, weight, and spacing of a character. It is the combination of pitch and
spacing.
Following code defines the font specified:
| Font font = new Font("Monotype Corsiva", Font.BOLD,
20) |
1. Class FontRenderContext measures the text specified.
2. A glyph
is an element of writing and displaying characters in a style. Class
GlyphVector provides a collection of glyphs containing geometric information.
Here is the code of SimpleFontPaintExample.java
import java.awt.*;
import javax.swing.*;
import java.awt.font.GlyphVector;
import java.awt.font.FontRenderContext;
public class SimpleFontPaintExample extends JPanel{
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
String st = "Java is an Object Oriented Programming Language.";
Font font = new Font("Monotype Corsiva", Font.BOLD, 20);
FontRenderContext fontRendContext = g2d.getFontRenderContext();
GlyphVector glyphVector = font.createGlyphVector(fontRendContext, st);
g2d.drawGlyphVector(glyphVector, 30, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Font Paint Example");
frame.getContentPane().add(new SimpleFontPaintExample());
frame.setSize(500, 150);
frame.show();
}
}
|
Output will be displayed as:

Download source Code

|