Home Java Example Java Swing Graphics2D Simple Font Paint Example



Simple Font Paint Example
Posted on: October 8, 2008 at 12:00 AM
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.

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

Related Tags for Simple Font Paint Example:
cdesigncomstyleiosizefontchardisplaypearappcharactercharactersifctenatspacingciearbiappearanceesignitdessplpecinceinmntplayispesspecpipppitchcombinationdescribebinsspdisplayingatpacplayingracishaeaandaractspecificweightzscrssrithbesstatiapdispesiesiisplayingplndonombomo


More Tutorials from this section

Ask Questions?    Discuss: Simple Font Paint Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.