如何获取转换文本的边界框(java)?

发布于 2024-09-11 12:04:28 字数 700 浏览 1 评论 0 原文

目前我有一个文本对象,我需要确定它的边界。我曾经使用图形对象来获取我尝试绘制的文本的字体规格,但由于我添加了旋转对象(可能还有更多)的功能,我需要一种更好的方法来获取该对象的边界。我已经看过很多地方,但到目前为止还没有什么对我真正有用的。这是我最近的尝试:

//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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-09-18 12:04:28

否,提供给 AffineTransform “nofollow noreferrer">FontRenderContext“用于将印刷点缩放到此 FontRenderContext 中的像素。”您应该能够使用 createTransformedShape() 在边界上以获得您想要的结果。

No, the AffineTransform supplied to FontRenderContext "is used to scale typographical points to pixels in this FontRenderContext." You should be able to use createTransformedShape() on the boundary to get the result you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文