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?
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:
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());
You can get the FontMetrics
object of the used font, and determine the ascent using getAscent()
or getMaxAscent()
, whichever is appropriate in your case.
Add FontMetrics.getAscent()
to the y position before rendering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With