Java AWT FontMetrics from Graphics
// FontMetrics and Graphics methods useful for obtaining font metrics. import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel { // display font metrics @Override/*from w w w .j a 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 static void main(String[] args) { // create frame for Main JFrame frame = new JFrame("Demonstrating FontMetrics"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main Main = new Main(); frame.add(Main); frame.setSize(510, 240); frame.setVisible(true); } }