Home Java Example Java Swing Graphics2D Outlining the font



Outlining the font
Posted on: October 8, 2008 at 12:00 AM
By outlining the font , we make the characters more stylish and impressive. We are providing you an example which outlines the text.

Outlining the font

     

This section illustrates you how to outline the font.

By outlining the font , we make the characters more stylish and impressive. We are providing you an example which outlines the text. The Font class represents fonts to render text in a visible way. The class FontRenderContext measures the text specified.

Following code defines the font:

Font font = new Font("Monotype Corsiva", 2, 50);

To give the graphical representation of styled character data, we have used the class TextLayout. The class AffineTransform provides the properties like translation, shearing, scaling, and rotation. The getBounds() method provides the boundaries displayed on the design surface. The method setClip(shape) sets the clipping of text specified as shape.

Following code outlines the string specified:

Shape shape = text.getOutline(null);

Here is the code of FontOutline.java 

import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;

public class FontOutline extends JApplet {
  public void initialize() {
  FontPaint font1 = new FontPaint();
  getContentPane().add(font1, BorderLayout.CENTER);
  }
 public static void main(String[] args) {
  FontPaint font2 = new FontPaint();
  JFrame frame = new JFrame("Outlining Font");
  frame.getContentPane().add(font2, BorderLayout.CENTER);
  frame.setSize(new Dimension(350200));
  frame.setVisible(true);
  }
}
class FontPaint extends JPanel {
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  setBackground(Color.white);
  int w = getSize().width;
  int h = getSize().height;
  Graphics2D g2d = (Graphics2D) g;
  FontRenderContext fontRendContext = g2d.getFontRenderContext();
  Font font = new Font("Monotype Corsiva"250);
  String st = new String("Hello World.");
  TextLayout text = new TextLayout(st, font, fontRendContext);
  AffineTransform affineTransform = new AffineTransform();
  Shape shape = text.getOutline(null);
  Rectangle rect = shape.getBounds();
  affineTransform = g2d.getTransform();
  affineTransform.translate(w / - (rect.width / 2), h / 2
  + (rect.height / 2));
  g2d.transform(affineTransform);
  g2d.setColor(Color.red);
  g2d.draw(shape);
  g2d.setClip(shape);
  }
}

Output will be displayed as:

Download Source Code

 

Related Tags for Outlining the font:
cclasstextfontsmakefontcharvicontextlineidrendercharactercharactersiftexieexamplectetoiniexamlinescissishextevisiblelipeiminasmntoutoutlinetroutliningclesspecendrcmeproxawhichxampssuspessracntextkishaimpressmeasureivmplpresspreeaandaractreprxtssrenthstpleplprndono


More Tutorials from this section

Ask Questions?    Discuss: Outlining the font  

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.