Gregory:
The following paragraph from the class documentation for TextLayout is pertinent:
"All graphical information returned from a TextLayout object's methods is relative to the origin of the TextLayout, which is the intersection of the TextLayout> object's baseline with its left edge. Also, coordinates passed into a TextLayout object's methods are assumed to be relative to the TextLayout object's origin. Clients usually need to translate between a TextLayout object's coordinate system and the coordinate system in another object Graphics object)."
The bounds argument to hitTestChar does not tell the call where the origin of the TextLayout is. The bounds serves to determine whether a hit is within the line or outside of it, and thus either before the first or after the last logical character. (In previous releases the bounds also affected the angle and position of carets on an italic/non-italic boundary, and thus which character a particular point would be associated with. I believe this latter behavior is no longer true in 1.5-- hit-testing was reworked to deal with superscripts and subscripts-- but I need to check.)
So in your case, you need to subtract the origin at which you are rendering the TextLayout from the x,y coordinate that you are hit testing it with, so that the point is relative to the left/baseline of the TextLayout. If you need to provide a different bounds from the TextLayout's default bounds, that too should be relative to the left/baseline of the TextLayout.
Doug
At 06:54 PM 9/2/2004, Gregory Pierce wrote:
---------------------- Information from the mail header ----------------------- Sender: Discussion list for Java 2D API <[EMAIL PROTECTED]> Poster: Gregory Pierce <[EMAIL PROTECTED]> Subject: Anyone got TextLayout adn TextHitInfo to work -------------------------------------------------------------------------------
Okay, here's the deal. I'm having some trouble getting TextHitInfo to return me valid data.
I have a TextLayout that I'm drawing centered within another component using:
textLayout.draw( g2d, (int)(bounds.getMinX() + bounds.getWidth()/2 - stringWidth/2), (int)(bounds.getMinY() + bounds.getHeight()/2 + stringHeight/2) );
Where bounds is the bounding box of the containing object. This works great. The text is centered in my object.
Next I try to actually perform a hit test on the text by performing:
Rectangle2D hitBounds = new Rectangle2D.Double( textLayoutBounds.getMinX() + bounds.getMinX(),
textLayoutBounds.getMinY() + bounds.getMinY(),
textLayoutBounds.getWidth(),
textLayoutBounds.getHeight() );
textHitInfo = textLayout.hitTestChar( x, y, hitBounds );
I create my own hitbounds because after confinued testing, the textLayout.getBounds always remains at x=0, y=0 which is just wrong and is a bug that I will report shortly. The bounds of the textLayout aren't at the origin - they never are.
4508 [AWT-EventQueue-0] DEBUG PanelWidget - java.awt.geom.Rectangle2D$Float[x=0.0,y=0.0,w=56.0,h=8.0] 4510 [AWT-EventQueue-0] DEBUG PanelWidget - x 223,161, bounds: java.awt.geom.Rectangle2D$Double[x=171.0,y=132.0,w=56.0,h=8.0] 4511 [AWT-EventQueue-0] DEBUG PanelWidget - Hit text - insert at = 8
Moving on. When I perform the hit test, I'm never getting the right character insert position (hitInfo.getInsertionIndex()) unless the text is sitting at the origin.
Any ideas?
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".