2017-01-12 12 views
-1

1
charater의 절대 위치가 필요합니다. 다음과 같습니다.
editorPane.getFontMetrics(f).getAscent()
기준선까지만 상대 거리가 있습니다. 어쩌면 기준선의 절대 위치를 얻는 방법이 있을까요? JEditorPane에서 charater의 절대 위치 가져 오기

편집

: 이 modelToView를 호출하고 바닥 FO에서 하강과 상승을 빼서 기준 *, 당신은 캐릭터의 시각적 인 위치를 계산할 수 라인 주에있는 모든 글꼴 이후 rect.y + rect.height - metrics.getDescent() - metrics.getAscent()

+0

JEditorPane의 [modelToView] (http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#modelToView-int-) 메소드를 호출하십시오. – VGR

+0

ModelToView가 작동하지 않습니다. y 값으로 0을 반환합니다. 높이는 "세계"와 같습니다. "Hello"라는 단어의 시작 부분의 y 좌표가 필요합니다. – Andreas

+1

뷰 rectangle의 하단에서 계산해야합니다 :'rect.y + rect.height-metrics.getDescent() - metrics.getAscent()'(여기서'rect'는 modelToView에 의해 반환 된 값입니다) – VGR

답변

0

의 결과입니다 직사각형

여러 글꼴이 관련되어 있으므로 분명히 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()); 
} 
+0

좋아 보인다. 월요일에해볼 게. Ty – Andreas

+0

작동하지 않습니다 :/내 대답을보십시오. – Andreas

0

RTFEditorKit을 사용하고 있습니다. 그래서 나는 코드를 바꿨다 :

static Point getLocation(int pos, 
      JEditorPane editorPane) 
      throws BadLocationException { 

     View view = editorPane.getUI().getRootView(editorPane); 
     int index; 
     while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) { 
      view = view.getView(index); 
     } 

     AttributeSet set = view.getAttributes(); 
     if (set != null && set.getAttribute(StyleConstants.FontFamily) != null) { 
      Font f = new Font((String) set.getAttribute(StyleConstants.FontFamily), Font.PLAIN, (Integer) set.getAttribute(StyleConstants.FontSize)); 
      FontMetrics metrics = editorPane.getFontMetrics(f); 

      Rectangle rect = editorPane.modelToView(pos); 
      return new Point(rect.x, 
        rect.y + rect.height - metrics.getDescent() - metrics.getAscent()); 
     } 
     else { 
      return new Point(0, 0); 
     } 
    } 

그러나 나는 항상 가장 큰 글꼴의 아 센트가 아닌 현재 Font의 아 센트를 얻는다.

+0

그게 네가 원하는게 아니야? 원하는 위치의 글꼴이 오르는 것이 아니라 줄에 가장 큰 글꼴이 항상 오르기를 원한다고 말하는 것입니까? – VGR

+0

그래, 가장 큰 글꼴의 상승이 필요해. 그러나 나는 항상 현재 위치의 상승을 얻는다. – Andreas

+0

이것은 작은 글꼴의 상단을 가리키는 빨간색 화살표가있는 질문의 이미지와 충돌하는 것 같습니다.또한, 당신은 코멘트 : "ModelToView가 작동하지 않습니다. y 값으로 0을 반환합니다. 높이는 "세계"와 같습니다. "안녕하세요"라는 단어의 시작 부분에 y 좌표가 필요합니다. "따라서 큰 글꼴로 된 텍스트의 y 좌표 또는 작은 글꼴로 된 텍스트의 y 좌표가 필요합니까? – VGR