5

I am displaying some graphics text on Screen by using the drawString(. . .) function of Java2D Library.

Refering to the figure in this article i want my string to be drawn from Ascender Line rather than BaseLine. In simple words is there any way to calculate the height b/w ascender line and Base Line?

Jame
  • 21,150
  • 37
  • 80
  • 107

3 Answers3

7

A normal drawString will align the base-line with the y-argument. If you want to draw the string so that the ascent-line is align with y, you need to pass y + fm.getAscent() where fm is the current FontMetrics object. See example below.

This screen shot:

enter image description here

is produced by this code:

FontMetrics fm = g.getFontMetrics();

g.setColor(Color.RED);
g.drawLine(10, 10, 100, 10);

g.setColor(Color.BLACK);
g.drawString("Hello frog", 10, 10 + fm.getAscent());
aioobe
  • 413,195
  • 112
  • 811
  • 826
3

You can get the FontMetrics object of the used font, and determine the ascent using getAscent() or getMaxAscent(), whichever is appropriate in your case.

king_nak
  • 11,313
  • 33
  • 58
1

Add FontMetrics.getAscent() to the y position before rendering.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820