Outlining the font

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

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

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