Java examples for 2D Graphics:Text
Getting the Dimensions of Text From within a component
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(); } }