Tag Archive for 'Java'

Text2D class

At work, I’ve been working on a new Java3D project.  Java3D has been great so far, and I’ve gotten a simple visualization program up and running in two weeks.  Today, I had to figure out how to determine the actual width of a Text2D object.  Creating a BoundingBox with the Text2D object’s getBounds() method doesn’t actually help, since it seems the dimensions are always a power of two.  I needed the actual width (the height can be calculated by multiplying the font size by the the rectangle scale factor), but the bounds returned didn’t give the the value I needed; it was close, but I needed to position the text to create an annotated axis.

My solution?  I had to rewrite the Text2D class.  I read a forum that mentioned this as a solution, but it didn’t give the code or how to do it.  If you go to the Java3D website (https://java3d.dev.java.net), you can browse the source code.  The Text2D class is located in the j3d-core-utils subproject.  I just copied the source file, added two private variables (height and width), created accessor methods getWidth and getHeight, and stored the height and width when it is calculated in the setupImage method.  Just change the package name and you can use the new class the same way the regular Text2D class is used.

What I don’t understand is, why didn’t Sun or the community in general do this?  It’s a really simple fix.  How do people even use the class if they can’t accurately determine how much space the actual text uses?  I’ll post the code (with the simple modifications) tomorrow.

Text2D.java