Wednesday, 10 February 2010

Freakin' text drawing stuff

It got tricky - trying to make the text component in the model aware of the rectangle it sits within for hit testing for mouse actions. Took time out to read the J2SE Graphics2D tutorials etc, some other stuff on the now Oracle / Sun site. There's AttributedStrings, there's TextLayouts, there's simple Font and drawString, but there's nothing that discusses the best widget to use.

I've gone for simple Font and draw, using FontMetrics to get the real estate it covers. Unfortunately every widget I've looked at needs to have the graphics object available which is only there it seems on the draw method - not sure how to get it any other time so am using the opportunity when draw() is called to update some statics about size, rectangles etc.

When a string is drawn the coords aren't the top left of the logical rectangle it occupies but at the bottom left of the first character, but not quite the bottom, there's Ascent, Descent and other stuff I need to get more familiar with but needless to say there's some more complexity going in to the object to cater for this and maintain the concept of a hit rectangle with other adornments, eg jpeg etc.

Nearly there ....

No comments:

Post a Comment