This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am trying to create a PNG image out of a text that the user entered. The image height is user configurable.
1. Get the text attributes as an AttributedString 2. Create a textLayout instance using the AttributedString and a FontRendererContext.
3. Get the bounds from the textLayout.
4. Create a Buffered Image of height = userdefined and width= textLayout.bounds.width
5. Get the graphics2D object from the buffered image.
6. Add scaling depending on the height.
7. Draw the textLayout on the image and write it into a file.
The problem I am facing is finding the Y position to start drawing from using the textlayout.draw method [Step 7].
I have not been able to get down to a generalized formula that would work with all fonts.
What I see is sometimes the text is clipped [ either @ the top or @ the bottom].
Any help on this regard will be greatly appreciated.
I am adding the code which creates the png.
Check out the FontMetrics class. It should help you figure out the width/height of a String when rendered using a particular Font
Joined: May 25, 2010
Thanks for the tip.
But FontMetrics did not help out much. The whole problem seems to be the scaling. Once scaled I seem to loose control as to where it should start rendering the text.
I modified the image drawing to use fontMetrics
Attached is the png file the above created with Arial Bold Font