Home Java Example Java Swing Graphics2D Simple Font Paint Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Simple Font Paint Example

Advertisement
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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2008

Ask Questions?    Discuss: Simple Font Paint Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com