【发布时间】:2011-03-21 21:14:54
【问题描述】:
目前我有一个需要确定其边界的文本对象。我曾经使用图形对象来获取我要绘制的文本的字体度量,但是由于我添加了旋转对象的功能(可能还有更多),我需要一种更好的方法来获取该对象的边界。我已经看过多个地方,但到目前为止还没有什么对我有用。这是我最近的尝试:
//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());
这是获取转换后文本边界框的正确方法吗?
【问题讨论】:
-
重新格式化的代码;如果不正确,请恢复。
标签: java user-interface swing text bounding-box