FontMetrics and Graphics methods useful for obtaining font metrics. - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

FontMetrics and Graphics methods useful for obtaining font metrics.

Demo Code

import javax.swing.JFrame;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;

 class MetricsJPanel extends JPanel 
{
   // display font metrics
   @Override//  w ww .  ja v  a 2  s  . c  o m
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g); 

      g.setFont(new Font("SansSerif", Font.BOLD, 12));
      FontMetrics metrics = g.getFontMetrics();
      g.drawString("Current font: " + g.getFont(), 10, 30);
      g.drawString("Ascent: " + metrics.getAscent(), 10, 45);
      g.drawString("Descent: " + metrics.getDescent(), 10, 60);
      g.drawString("Height: " + metrics.getHeight(), 10, 75);
      g.drawString("Leading: " + metrics.getLeading(), 10, 90);

      Font font = new Font("Serif", Font.ITALIC, 14);
      metrics = g.getFontMetrics(font);
      g.setFont(font);
      g.drawString("Current font: " + font, 10, 120);
      g.drawString("Ascent: " + metrics.getAscent(), 10, 135);
      g.drawString("Descent: " + metrics.getDescent(), 10, 150);
      g.drawString("Height: " + metrics.getHeight(), 10, 165);
      g.drawString("Leading: " + metrics.getLeading(), 10, 180);
   } 
}

public class Main 
{
   // execute application
   public static void main(String[] args)
   {
      // create frame for MetricsJPanel
      JFrame frame = new JFrame("Demonstrating FontMetrics");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      MetricsJPanel metricsJPanel = new MetricsJPanel(); 
      frame.add(metricsJPanel); 
      frame.setSize(510, 240);
      frame.setVisible(true);
   } 
}

Related Tutorials