Skip to main content

[JAVA2D] Why does TextLayout operate different from the rest of Java2D?

1 reply [Last post]
Anonymous

I've been messing around with TextLayout for a while now and have
become increasingly annoyed with the semantics of TextLayout in that
it doesn't operate like the rest of the stuff in Java2D.

Graphics2D has the ability to draw everything else I've thrown at it
except a TextLayout. TextLayout for some reason that just escapes me
lives in its own coordinate space and must be translated into place in
order to be drawn. Then you have to translate back to where you were
so you don't screw up the rest of your drawings. My code is full of
stuff like

g2d.translate( textX, textY );

textA.draw( g2d, 0, 0 );

bounds = textA.getBounds();

g2d.translate( -textX, - textY );

g2d.setColor( textBFontColor );

// compute textBX, textBY

g2d.translate( textBX, textBY );

textB.draw( g2d, 0, 0 );

g2d.translate( -textBX, -textBY );

Seems to be a lot of work as opposed to:

g2d.draw( textA, textX, textY );
g2d.setColor( textBFontColor );
g2d.draw( textB, textBX, textBY );

Maybe I'm missing the reason for having TextLayout live off on its
own. The problem becomes more compounded when you want to do hit
testing on characters or draw the carets that would result from the
hit test. Am I missing something, or is this a piece of API that needs
to be 'updated' to be more in line with the rest of Graphics2D.

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jim Graham

I'll wait for one of the text gurus to respond about the reason that
TextLayout needed to control its own rendering, but at least one part
of your example can be simplified:

> g2d.translate( textX, textY );
> textA.draw( g2d, 0, 0 );
> g2d.translate( -textX, - textY );

Could be written as simply:

textA.draw(g2d, textX, textY);

...jim

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".