目前我有一个文本对象,我需要确定它的边界。我曾经使用图形对象来获取我尝试绘制的文本的字体规格,但由于我添加了旋转对象(可能还有更多)的功能,我需要一种更好的方法来获取该对象的边界。我已经看过很多地方,但到目前为止还没有什么对我真正有用的。这是我最近的尝试:
//This is the bounding box edges 0: left, 1: right 2: top 3: bottom
int toReturn[] = new int[4];
//this.transform is the AffineTransform for the text Object(currently only
//rotated)
FontRenderContext frc = new FontRenderContext(this.transform,true,false);
TextLayout tl = new TextLayout(this.typedText,this.font,frc);
Rectangle2D bb = tl.getBounds();
toReturn[0] = (int)(bb.getX());
toReturn[1] = (int)(bb.getX()+bb.getWidth());
toReturn[2] = (int)(bb.getY());
toReturn[3] = (int)(bb.getY()+bb.getHeight());
这是获取转换文本的边界框的正确方法吗?
Currently I have a text object that I need to determine its bounds. I used to use the graphics object to obtain the font metrics of the text I am trying to draw, but since I added functionality to rotating the object(and possibly more) I need a better way to get the bounds of this object. I have looked multiple places and nothing has really worked for me as of yet. Here is my most current attempt:
//This is the bounding box edges 0: left, 1: right 2: top 3: bottom
int toReturn[] = new int[4];
//this.transform is the AffineTransform for the text Object(currently only
//rotated)
FontRenderContext frc = new FontRenderContext(this.transform,true,false);
TextLayout tl = new TextLayout(this.typedText,this.font,frc);
Rectangle2D bb = tl.getBounds();
toReturn[0] = (int)(bb.getX());
toReturn[1] = (int)(bb.getX()+bb.getWidth());
toReturn[2] = (int)(bb.getY());
toReturn[3] = (int)(bb.getY()+bb.getHeight());
Is this the proper way to get the bounding box for transformed text?
发布评论
评论(1)
否,提供给 AffineTransform “nofollow noreferrer">
FontRenderContext
“用于将印刷点缩放到此 FontRenderContext 中的像素。”您应该能够使用createTransformedShape()
在边界上以获得您想要的结果。No, the
AffineTransform
supplied toFontRenderContext
"is used to scale typographical points to pixels in this FontRenderContext." You should be able to usecreateTransformedShape()
on the boundary to get the result you want.