Get GlyphVector from font - Java 2D Graphics

Java examples for 2D Graphics:GlyphVector

Description

Get GlyphVector from font

Demo Code



import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

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

public class Main extends JFrame
{
     public static void main(String[] arg)
     {/* ww  w  .  j  a  va 2s .c  om*/
          JFrame frame = new Main();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.pack();
         frame.setVisible(true);
     }
     
     public Main()
     {
          super("Programming Exercise 3.15");
          JPanel contentPanel = new JPanel(new FlowLayout());
          JPanel panel = new DrawIt();
          panel.setPreferredSize(new Dimension(600,600));
          contentPanel.add(panel);
          add(contentPanel);
     }
     
     class DrawIt extends JPanel
     {
          public void paintComponent(Graphics g)
          {
               super.paintComponent(g);
               Graphics2D g2 = (Graphics2D)g;
               g2.translate(300, 300);
               
               Font font = new Font("Serif", Font.BOLD, 60);
               g2.setFont(font);
               FontRenderContext frc = g2.getFontRenderContext();
               GlyphVector gvN = font.createGlyphVector(frc, "N");
               GlyphVector gvY = font.createGlyphVector(frc, "Y");
               Shape glyphN = gvN.getOutline(0,0);
               Shape glyphY = gvY.getOutline(30,0);
               g2.draw(glyphN);
               g2.draw(glyphY);
          }
     }
}

Related Tutorials