Getting the Dimensions of Text From within a component - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Getting the Dimensions of Text From within a component

Demo Code


import java.awt.Font;
import java.awt.FontMetrics;

import javax.swing.JComponent;

class MyComponent extends JComponent {
  MyComponent() {// www .  j  av a  2 s.c  o  m
    Font font = new Font("Serif", Font.PLAIN, 12);
    FontMetrics fontMetrics = getFontMetrics(font);

    int width = fontMetrics.stringWidth("aString");
    int height = fontMetrics.getHeight();
  }
}

Related Tutorials