Outline Font paint : Font « 2D Graphics GUI « Java

Outline Font paint

Outline Font paint

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontPaint extends JApplet {
  public void init() {
    FontPanel fontPanel = new FontPanel();
    getContentPane().add(fontPanel, BorderLayout.CENTER);

  public static void main(String[] args) {
    FontPanel starPanel = new FontPanel();

    JFrame f = new JFrame("Font");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

    f.getContentPane().add(starPanel, BorderLayout.CENTER);
    f.setSize(new Dimension(550, 200));

class FontPanel extends JPanel {
  public void paintComponent(Graphics g) {
    int width = getSize().width;
    int height = getSize().height;
    Graphics2D g2 = (Graphics2D) g;



    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, 60);
    String s = new String("Java Source and Support.");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle outlineBounds = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(width / 2 - (outlineBounds.width / 2), height / 2
        + (outlineBounds.height / 2));


Related examples in the same category

1.Ascii font pattern
2.A cache of the dynamically loaded fonts found in the fonts directory
3.Load font from ttf file
4.Font paintFont paint
5.Font DerivationFont Derivation
6.Font centeredFont centered
7.Font ListFont List
8.FontDemo lists the system fonts and provides a sample of each one(Have some problems)
9.Finds and displays available fonts
10.Obtain FontMetrics of different fonts
11.Draw font inside a Rectangle
12.Display font in a grid
13.Create font from true type font
14.Wrap string according to FontMetrics
15.Draw base line and enclosing line for a font
16.List all Fonts