문제

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