java - Getting text bounds within JavaFX -


i using javafx produce bitmap previews of music documents.

at points, within paint process, need know dimensions of given string.

upto now, have used following :

bounds = textbuilder.create().text(string).font(m_font).build().getlayoutbounds(); 

however, eclipse informs me depreciated. depreciated bounds object empty (all set zero).

how go getting bounds - width , height - of single line string ?

please note there no on screen controls being used display stuff. generated in memory , "dumped" out png bitmap.

i have scavenged around on net , have not found answer (or missed entirely).

any expert available ?

as mentioned in comment, getlayoutbounds() not deprecated , valid use. it's builder deprecated.

that said, have created following test application produced seemingly correct output, using builders , 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());          // bad font, 0 sized         font my_font = new font("i not font", 0);         text text = new text("heeeeello");         text.setfont(my_font);         b = text.getlayoutbounds();         system.out.println(b.getheight() + ", " + b.getwidth());          // bad font, arbitrary size         my_font = new font("i not 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 hypothesise your font screwing things up, possibly size set 0 or other error.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -