下面列出了javafx.scene.text.Text#getLayoutBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public FontDetails (String name, int size, Font font)
{
this.font = font;
this.name = name;
this.size = size;
Text text = new Text ("W");
text.setFont (font);
Bounds bounds = text.getLayoutBounds ();
height = (int) (bounds.getHeight () + 0.5);
width = (int) (bounds.getWidth () + 0.5);
ascent = (int) (-bounds.getMinY () + 0.5);
descent = height - ascent;
}
@Override
public java.awt.Dimension getBounds(String s)
{
Font currentFont = ctx.getFont();
Text t = new Text(s);
t.setFont(currentFont);
Bounds b = t.getLayoutBounds();
java.awt.Dimension bounds = new java.awt.Dimension((int)b.getWidth(), (int)b.getHeight());
return bounds;
}
/**
* Draws the string inside a given rectangle.
* @param pString The string to draw.
* @param pGraphics the graphics context
* @param pRectangle the rectangle into which to place the string
*/
public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle)
{
Text label = getLabel(pString);
pGraphics.setTextAlign(label.getTextAlignment());
int textX = 0;
int textY = 0;
if(aAlignment == Align.CENTER)
{
textX = pRectangle.getWidth()/2;
textY = pRectangle.getHeight()/2;
pGraphics.setTextBaseline(VPos.CENTER);
}
else
{
pGraphics.setTextBaseline(VPos.TOP);
textX = HORIZONTAL_TEXT_PADDING;
}
pGraphics.translate(pRectangle.getX(), pRectangle.getY());
ViewUtils.drawText(pGraphics, textX, textY, pString.trim(), getFont());
if(aUnderlined && pString.trim().length() > 0)
{
int xOffset = 0;
int yOffset = 0;
Bounds bounds = label.getLayoutBounds();
if(aAlignment == Align.CENTER)
{
xOffset = (int) (bounds.getWidth()/2);
yOffset = (int) (getFont().getSize()/2) + 1;
}
else if(aAlignment == Align.RIGHT)
{
xOffset = (int) bounds.getWidth();
}
ViewUtils.drawLine(pGraphics, textX-xOffset, textY+yOffset,
(int) (textX-xOffset+bounds.getWidth()), textY+yOffset, LineStyle.SOLID);
}
pGraphics.translate(-pRectangle.getX(), -pRectangle.getY());
}
private Bounds getBounds(String s)
{
Text t = new Text(s);
t.setFont(currentFont);
return t.getLayoutBounds();
}