- java.lang.Object
-
- java.awt.font.TextHitInfo
-
public final class TextHitInfo extends Object
TheTextHitInfoclass represents a character position in a text model, and a bias, or "side," of the character. Biases are either leading (the left edge, for a left-to-right character) or trailing (the right edge, for a left-to-right character). Instances ofTextHitInfoare used to specify caret and insertion positions within text.For example, consider the text "abc". TextHitInfo.trailing(1) corresponds to the right side of the 'b' in the text.
TextHitInfois used primarily byTextLayoutand clients ofTextLayout. Clients ofTextLayoutqueryTextHitInfoinstances for an insertion offset, where new text is inserted into the text model. The insertion offset is equal to the character position in theTextHitInfoif the bias is leading, and one character after if the bias is trailing. The insertion offset for TextHitInfo.trailing(1) is 2.Sometimes it is convenient to construct a
TextHitInfowith the same insertion offset as an existing one, but on the opposite character. ThegetOtherHitmethod constructs a newTextHitInfowith the same insertion offset as an existing one, with a hit on the character on the other side of the insertion offset. CallinggetOtherHiton trailing(1) would return leading(2). In general,getOtherHitfor trailing(n) returns leading(n+1) andgetOtherHitfor leading(n) returns trailing(n-1).Example:
Converting a graphical point to an insertion point within a text model
TextLayout layout = ...; Point2D.Float hitPoint = ...; TextHitInfo hitInfo = layout.hitTestChar(hitPoint.x, hitPoint.y); int insPoint = hitInfo.getInsertionIndex(); // insPoint is relative to layout; may need to adjust for use // in a text model
- See Also:
TextLayout
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TextHitInfoafterOffset(int offset)Creates aTextHitInfoat the specified offset, associated with the character after the offset.static TextHitInfobeforeOffset(int offset)Creates aTextHitInfoat the specified offset, associated with the character before the offset.booleanequals(TextHitInfo hitInfo)Returnstrueif the specifiedTextHitInfohas the samecharIndexandisLeadingEdgeas thisTextHitInfo.booleanequals(Object obj)Returnstrueif the specifiedObjectis aTextHitInfoand equals thisTextHitInfo.intgetCharIndex()Returns the index of the character hit.intgetInsertionIndex()Returns the insertion index.TextHitInfogetOffsetHit(int delta)Creates aTextHitInfowhose character index is offset bydeltafrom thecharIndexof thisTextHitInfo.TextHitInfogetOtherHit()Creates aTextHitInfoon the other side of the insertion point.inthashCode()Returns the hash code.booleanisLeadingEdge()Returnstrueif the leading edge of the character was hit.static TextHitInfoleading(int charIndex)Creates aTextHitInfoon the leading edge of the character at the specifiedcharIndex.StringtoString()Returns aStringrepresenting the hit for debugging use only.static TextHitInfotrailing(int charIndex)Creates a hit on the trailing edge of the character at the specifiedcharIndex.
-
-
-
Method Detail
-
getCharIndex
public int getCharIndex()
Returns the index of the character hit.- Returns:
- the index of the character hit.
-
isLeadingEdge
public boolean isLeadingEdge()
Returnstrueif the leading edge of the character was hit.- Returns:
trueif the leading edge of the character was hit;falseotherwise.
-
getInsertionIndex
public int getInsertionIndex()
Returns the insertion index. This is the character index if the leading edge of the character was hit, and one greater than the character index if the trailing edge was hit.- Returns:
- the insertion index.
-
hashCode
public int hashCode()
Returns the hash code.- Overrides:
hashCodein classObject- Returns:
- the hash code of this
TextHitInfo, which is also thecharIndexof thisTextHitInfo. - See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object obj)
Returnstrueif the specifiedObjectis aTextHitInfoand equals thisTextHitInfo.- Overrides:
equalsin classObject- Parameters:
obj- theObjectto test for equality- Returns:
trueif the specifiedObjectequals thisTextHitInfo;falseotherwise.- See Also:
Object.hashCode()
-
equals
public boolean equals(TextHitInfo hitInfo)
Returnstrueif the specifiedTextHitInfohas the samecharIndexandisLeadingEdgeas thisTextHitInfo. This is not the same as having the same insertion offset.- Parameters:
hitInfo- a specifiedTextHitInfo- Returns:
trueif the specifiedTextHitInfohas the samecharIndexandisLeadingEdgeas thisTextHitInfo.
-
toString
public String toString()
Returns aStringrepresenting the hit for debugging use only.
-
leading
public static TextHitInfo leading(int charIndex)
Creates aTextHitInfoon the leading edge of the character at the specifiedcharIndex.- Parameters:
charIndex- the index of the character hit- Returns:
- a
TextHitInfoon the leading edge of the character at the specifiedcharIndex.
-
trailing
public static TextHitInfo trailing(int charIndex)
Creates a hit on the trailing edge of the character at the specifiedcharIndex.- Parameters:
charIndex- the index of the character hit- Returns:
- a
TextHitInfoon the trailing edge of the character at the specifiedcharIndex.
-
beforeOffset
public static TextHitInfo beforeOffset(int offset)
Creates aTextHitInfoat the specified offset, associated with the character before the offset.- Parameters:
offset- an offset associated with the character before the offset- Returns:
- a
TextHitInfoat the specified offset.
-
afterOffset
public static TextHitInfo afterOffset(int offset)
Creates aTextHitInfoat the specified offset, associated with the character after the offset.- Parameters:
offset- an offset associated with the character after the offset- Returns:
- a
TextHitInfoat the specified offset.
-
getOtherHit
public TextHitInfo getOtherHit()
Creates aTextHitInfoon the other side of the insertion point. ThisTextHitInforemains unchanged.- Returns:
- a
TextHitInfoon the other side of the insertion point.
-
getOffsetHit
public TextHitInfo getOffsetHit(int delta)
Creates aTextHitInfowhose character index is offset bydeltafrom thecharIndexof thisTextHitInfo. ThisTextHitInforemains unchanged.- Parameters:
delta- the value to offset thischarIndex- Returns:
- a
TextHitInfowhosecharIndexis offset bydeltafrom thecharIndexof thisTextHitInfo.
-
-