I'm trying to place text accurately in the centre of a pane both horizontally and vertically. Using fontmetrics and a test program I get the following results:
This test raises the following questions:
The FontMetrics class defines a font metrics object, which encapsulates information about the rendering of a particular font on a particular screen.
Ascent - The recommended distance above the baseline for singled spaced text. Descent - The recommended distance below the baseline for singled spaced text. Bottom - The maximum distance below the baseline for the lowest glyph in the font at a given text size.
Font metrics are measurements of text rendered by a Font object such as the height of a line of text in the font. The most common way to measure text is to use a FontMetrics instance which encapsulates this metrics information.
April 2017) (Learn how and when to remove this template message) Fontmatrix is a font manager for Linux desktop environments. It can manage fonts installed system-wide or for individual user accounts. It relies on FreeType to render font samples, and on Qt for its user interface.
Here is an alternate implementation of Frank's reportSize
function:
public void reportSize(String s, Font myFont) {
Text text = new Text(s);
text.setFont(myFont);
Bounds tb = text.getBoundsInLocal();
Rectangle stencil = new Rectangle(
tb.getMinX(), tb.getMinY(), tb.getWidth(), tb.getHeight()
);
Shape intersection = Shape.intersect(text, stencil);
Bounds ib = intersection.getBoundsInLocal();
System.out.println(
"Text size: " + ib.getWidth() + ", " + ib.getHeight()
);
}
This implementation uses shape intersection to determine the size of the bounding box of the rendered shape with no whitespace. The implementation does not rely on com.sun
package classes which may not be directly accessible to user application code in Java 9+.
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