Java: Cómo dibujarString () desde la línea ascendente

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

Refering to the figure in this artículo 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?

preguntado el 08 de noviembre de 11 a las 13:11

3 Respuestas

Un 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, necesitas pasar y + fm.getAscent() dónde fm es la corriente 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());

Respondido 14 Jul 15, 12:07

Puede obtener la FontMetrics object of the used font, and determine the ascent using getAscent() or getMaxAscent(), whichever is appropriate in your case.

respondido 08 nov., 11:17

Añadir FontMetrics.getAscent() to the y position before rendering.

respondido 08 nov., 11:19

Shouldn't he add that value to the y before drawing the string? - aioobe

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.