I want to measure a "smallest unit" of a string (which is displayed in JTextArea with some font).

It's the width of a single character + the spacing after it (which depends on the next character).

Eg. I want to measure the pixel distance from beginning of char A to beginning of char Z in the string "AZ".

How can I do that? I tried using the FontMetrics and measuring the string width of both characters together and then decrementing it for the width of the second one but it didn't work...

有帮助吗?

解决方案

java.awt.Font has

public GlyphVector layoutGlyphVector(FontRenderContext frc,
                                     char[] text,
                                     int start,
                                     int limit,
                                     int flags) {

Then use public Point2D getGlyphPosition(int glyphIndex) method of GlyphVector and compare difference of X coordinates for any desired pair of chars.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top