[英]Getting text bounds within JavaFX
I am using JavaFX to produce bitmap previews of my music documents. 我正在使用JavaFX生成音乐文档的位图预览。
At certain points, within the paint process, I need to know the dimensions of a given string. 在绘制过程中的某些时候,我需要知道给定弦的尺寸。
Upto now, I have used the following : 到目前为止,我已经使用了以下内容:
bounds = TextBuilder.create().text(string).font(m_Font).build().getLayoutBounds();
However, eclipse informs me that this is depreciated. 但是,日食告诉我这已经贬值了。 It is so depreciated that the bounds object is empty (all set to zero).
它是如此贬值,以至于bounds对象为空(全部设置为零)。
How do I go about getting the bounds - width and height - of a single line string now ? 我现在该如何获取单个线串的边界(宽度和高度)?
Please note that there are no on screen controls being used to display this stuff. 请注意, 没有任何屏幕上的控件用于显示这些内容。 Everything is generated in memory and "dumped" out to a png bitmap.
一切都在内存中生成,然后“转储”到png位图。
I have scavenged around on the net and have not found the answer ( or I missed it entirely ). 我已经在网上扫荡了,却找不到答案( 或者我完全错过了答案)。
Any expert help available ? 有专家帮助吗?
As I mentioned in my comment, getLayoutBounds()
is not deprecated and is perfectly valid to use. 正如我在评论中提到的那样,不建议弃用
getLayoutBounds()
,并且完全可以使用。 It's just the builder which is deprecated. 只是过时的构建器。
That said, I have created the following test application which produced seemingly correct output, using builders and creating objects directly: 就是说,我创建了以下测试应用程序,使用构建器并直接创建对象,该应用程序产生了看似正确的输出:
import javafx.geometry.Bounds;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class stack extends javafx.application.Application {
public static void main(String[] args)
{
// Builder
Bounds b = TextBuilder.create().text("hello").build().getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
b = TextBuilder.create().text("heeeeello").build().getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// No builder
b = new Text("hello").getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
b = new Text("heeeeello").getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// With bad font, zero sized
Font my_font = new Font("i am not a font", 0);
Text text = new Text("heeeeello");
text.setFont(my_font);
b = text.getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// With bad font, arbitrary size
my_font = new Font("i am not a font", 20);
text = new Text("heeeeello");
text.setFont(my_font);
b = text.getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
}
@Override
public void start(Stage primaryStage) throws Exception { }
}
Output: 输出:
15.9609375, 25.91015625
15.9609375, 51.01171875
15.9609375, 25.91015625
15.9609375, 51.01171875
0.0, 0.0
26.6015625, 85.01953125
I would hypothesise that your font is screwing things up , possibly the size is set to zero or some other error. 我假设您的字体搞砸了 ,可能大小设置为零或其他错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.