의 결과입니다 직사각형
여러 글꼴이 관련되어 있으므로 분명히 JEditorPane의 getFont 메서드로는 충분하지 않습니다. HTMLDocument의 속성은 단순히 HTML 요소 자체를 모델링하기 때문에 문서의 원시 요소 속성도 충분하지 않습니다. 그러나, 모든 문서의 위치에 대한 실제 글꼴은 해당 View에서 얻을 수 있습니다 :
static Point getLocation(int pos,
JEditorPane editorPane)
throws BadLocationException {
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
View view = editorPane.getUI().getRootView(editorPane);
int index;
while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) {
view = view.getView(index);
}
AttributeSet attr = doc.getStyleSheet().getViewAttributes(view);
Font f = doc.getStyleSheet().getFont(attr);
FontMetrics metrics = editorPane.getFontMetrics(f);
Rectangle rect = editorPane.modelToView(pos);
return new Point(rect.x,
rect.y + rect.height - metrics.getDescent() - metrics.getAscent());
}
* 단순화하기 위해, 내가 매달려 기준선과 수직 기준선과 문자를 무시하고있다.
편집 : RTFEditorKit는 거의 사용되지 않습니다 때문에, 나는 잘못이는 HTMLEditorKit를 사용하고 가정한다. 이 기능은 RTF 문서에서 작동합니다.
static Point getLocation(int pos,
JEditorPane editorPane)
throws BadLocationException {
StyledDocument doc = (StyledDocument) editorPane.getDocument();
View view = editorPane.getUI().getRootView(editorPane);
int index;
while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) {
view = view.getView(index);
}
AttributeSet attr = view.getAttributes();
Font f = doc.getFont(attr);
FontMetrics metrics = editorPane.getFontMetrics(f);
Rectangle rect = editorPane.modelToView(pos);
return new Point(rect.x,
rect.y + rect.height - metrics.getDescent() - metrics.getAscent());
}
출처
2017-01-13 15:21:18
VGR
JEditorPane의 [modelToView] (http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#modelToView-int-) 메소드를 호출하십시오. – VGR
ModelToView가 작동하지 않습니다. y 값으로 0을 반환합니다. 높이는 "세계"와 같습니다. "Hello"라는 단어의 시작 부분의 y 좌표가 필요합니다. – Andreas
뷰 rectangle의 하단에서 계산해야합니다 :'rect.y + rect.height-metrics.getDescent() - metrics.getAscent()'(여기서'rect'는 modelToView에 의해 반환 된 값입니다) – VGR