Java 获取角色在绝地武士窗格中的绝对位置
Java 获取角色在绝地武士窗格中的绝对位置,java,swing,fonts,jeditorpane,baseline,Java,Swing,Fonts,Jeditorpane,Baseline,我需要角色的绝对位置。使用:editorPane.getFontMetrics(f.getAscent() 我只得到到基线的相对距离。也许有办法得到基线的绝对位置 编辑: 这是的结果,因为一行中的所有字体都共享一个基线*,您可以通过调用并减去矩形底部的下降和上升来计算角色的视觉位置 由于涉及多种字体,显然JEditorPane的getFont方法是不够的。文档的原始元素属性也不足,因为HTMLDocument的属性只是对HTML元素本身建模。但是,任何文档位置的实际字体都可以从相应的: *为了简
我需要角色的绝对位置。使用:
editorPane.getFontMetrics(f.getAscent()
我只得到到基线的相对距离。也许有办法得到基线的绝对位置 编辑:
这是的结果,因为一行中的所有字体都共享一个基线*,您可以通过调用并减去矩形底部的下降和上升来计算角色的视觉位置 由于涉及多种字体,显然JEditorPane的getFont方法是不够的。文档的原始元素属性也不足,因为HTMLDocument的属性只是对HTML元素本身建模。但是,任何文档位置的实际字体都可以从相应的: *为了简单起见,我忽略了具有悬挂基线和垂直基线的字符 编辑:由于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());
}
我正在使用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);
}
}
但是我总是得到当前字体的上升,而不是最大字体的上升。调用JEditorPane的方法。ModelToView不起作用。它为y值返回0。高度就像“世界”这个词。我需要单词“Hello”开头的y坐标。您需要从视图矩形的底部计算它:
rect.y+rect.height-metrics.getDescent()-metrics.getAscent()
(其中rect
是modelToView返回的值)抱歉,它不起作用。上升+下降+领先=高度。这是荧光灯的高度。所以我错过了荧光灯下的空间。你是如何获得用来获得FontMetrics的字体的?这不是你想要的吗?你是说你一直想要线条上最大字体的上升,而不是在所需位置上的上升?是的,我需要最大字体的上升。但我总是得到当前位置的上升。这似乎与你问题中的图像冲突,它有一个红色箭头指向较小字体的顶部。此外,您还评论说:“ModelToView不起作用。它返回0作为y值。高度类似于单词“World”。我需要单词“Hello”开头的y坐标。“那么,您想要大字体文本的y坐标,还是小字体文本的y坐标?我想要将插入符号设置到正确的位置(箭头)。y坐标是r.y+(最大字体的上升-当前字体的上升)。如果我没弄错的话,您需要一个文本光标,该光标与行中最高的字体一样高,而不管光标位置的字体大小。对的
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);
}
}