fr.lri.swingstates.canvas
Class CText

java.lang.Object
  extended by fr.lri.swingstates.canvas.CShape
      extended by fr.lri.swingstates.canvas.CText
All Implemented Interfaces:
CElement, java.lang.Cloneable

public class CText
extends CShape

A text shape for use with an Canvas. The text is drawn according to the fill paint and the text font. If the shape is outlined, the bounding box of the text is drawn according to the outline paint and stroke.

Author:
Caroline Appert

Field Summary
static java.awt.font.FontRenderContext FRC
          The FontRenderContext used to render this text.
 
Constructor Summary
CText(java.awt.geom.Point2D loc, java.lang.String txt, java.awt.Font f)
          Builds a SMtext.
 
Method Summary
 CShape copyTo(CShape sms)
          Copies this shape into a destination shape.
 CShape duplicate()
          Creates a new copy of this shape and returns it.
 CEllipse getAbsoluteEllipse(double w, double h)
          Returns a CEllipse that surrounds this text.
 CRectangle getAbsoluteRectangle(double w, double h)
          Returns a CRectangle that surrounds this text.
 char getChar(double x, double y)
          Returns the nearest char from the location (x, y).
 char getChar(java.awt.geom.Point2D p)
          Returns the nearest char from the point p.
 java.awt.Font getFont()
          Returns the text font.
 CEllipse getRelativeEllipse(double ratioX, double ratioY)
          Returns a CEllipse that surrounds this text.
 CRectangle getRelativeRectangle(double ratioX, double ratioY)
          Returns a CRectangle that surrounds this text.
 java.lang.String getText()
          Returns the text.
 void paint(java.awt.Graphics g)
          Paints the shape.
 CElement setAntialiased(boolean a)
          Specifies whether this shape is antialiased.
 CText setFont(java.awt.Font f)
          Sets the text font.
 CText setReferencePointToBaseline()
          Sets the reference point of this CText to its baseline.
 CText setText(java.lang.String text)
          Sets the text to be displayed.
 
Methods inherited from class fr.lri.swingstates.canvas.CShape
above, aboveAll, addChild, addGhost, addTag, addTag, addTag, addTo, animate, asEllipse, asImage, asPolyLine, asRectangle, asRectangularShape, asText, attachSM, below, belowAll, canvasToShape, clone, contains, contains, contains, contains, detachSM, duplicateWithTags, firstShape, fixReferenceShapeToCurrent, getAbsTransform, getAntialiasedShapes, getBoundingBox, getCanvas, getCenterX, getCenterY, getChild, getChildren, getChildrenCount, getClip, getFilledShapes, getFillPaint, getFirstAntialiasedShape, getFirstFilledShape, getFirstHavingTag, getFirstOutlinedShape, getGhost, getHeight, getHierarchy, getIntersection, getMaxX, getMaxY, getMinX, getMinY, getOutlinedShapes, getOutlinePaint, getParent, getReferenceX, getReferenceY, getRenderingHint, getRotation, getScaleX, getScaleY, getShape, getStroke, getSubtraction, getTransform, getTranslateX, getTranslateY, getTransparencyFill, getTransparencyOutline, getWidth, hasTag, hasTag, indexOf, intersects, isAbove, isAntialiased, isBelow, isDrawable, isFilled, isOnOutline, isOutlined, isPickable, pickStyle, remove, removeAllChildren, removeChild, removeChild, removeGhost, removeTag, removeTag, rotateBy, rotateTo, scaleBy, scaleBy, scaleTo, scaleTo, setClip, setDrawable, setFilled, setFillPaint, setOutlined, setOutlinePaint, setParent, setPickable, setReferencePoint, setReferencePoint, setRenderingHint, setShape, setStroke, setTransformToIdentity, setTransparencyFill, setTransparencyFill, setTransparencyOutline, setTransparencyOutline, shapeToCanvas, translateBy, translateTo
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FRC

public static java.awt.font.FontRenderContext FRC
The FontRenderContext used to render this text.

Constructor Detail

CText

public CText(java.awt.geom.Point2D loc,
             java.lang.String txt,
             java.awt.Font f)
Builds a SMtext.

Parameters:
loc - The lower left point of this CText.
txt - The text of this CText.
f - The font of this CText.
Method Detail

paint

public void paint(java.awt.Graphics g)
Paints the shape. Normally this method need not be called, since the canvas paints the display list automatically.

Overrides:
paint in class CShape
Parameters:
g - The graphics with which the shape must be painted.

getChar

public char getChar(double x,
                    double y)
Returns the nearest char from the location (x, y).

Parameters:
x - The x-coordinate (in the canvas coordinate system)
y - The y-coordinate (in the canvas coordinate system)
Returns:
The nearest char.

getChar

public char getChar(java.awt.geom.Point2D p)
Returns the nearest char from the point p.

Parameters:
p - The pont (in the canvas coordinate system)
Returns:
The nearest char.

getRelativeRectangle

public CRectangle getRelativeRectangle(double ratioX,
                                       double ratioY)
Returns a CRectangle that surrounds this text. If the bounding box of the text is a box whose width and height are respectively w and h, the result rectangle is centered at the center of the text and has a width ratioX*w and a height ratioY*h. This method does NOT add the built CRectangle to the canvas.

Parameters:
ratioX - The width ratio.
ratioY - The height ratio.
Returns:
The surrounding CRectangle.

getRelativeEllipse

public CEllipse getRelativeEllipse(double ratioX,
                                   double ratioY)
Returns a CEllipse that surrounds this text. If the bounding box of the text is a box whose width and height are respectively w and h, the result ellipse is centered at the center of the text and has a width ratioX*w and a height ratioY*h. This method does NOT add the built CRectangle to the canvas.

Parameters:
ratioX - The width ratio.
ratioY - The height ratio.
Returns:
The surrounding CEllipse.

getAbsoluteRectangle

public CRectangle getAbsoluteRectangle(double w,
                                       double h)
Returns a CRectangle that surrounds this text. The result rectangle is centered at the center of the text and has a width w and a height h. This method does NOT add the built CRectangle to the canvas.

Parameters:
w - The width ratio.
h - The height ratio.
Returns:
The surrounding CRectangle.

getAbsoluteEllipse

public CEllipse getAbsoluteEllipse(double w,
                                   double h)
Returns a CEllipse that surrounds this text. The result ellipse is centered at the center of the text and has a width w and a height h. This method does NOT add the built CRectangle to the canvas.

Parameters:
w - The width ratio.
h - The height ratio.
Returns:
The surrounding CEllipse.

getText

public java.lang.String getText()
Returns the text.

Returns:
the text.

setReferencePointToBaseline

public CText setReferencePointToBaseline()
Sets the reference point of this CText to its baseline.

By default, the position of a CText specifies the upper left corner of its bounding box. For example, the following lines makes the two CText be displayed below the segment [(10, 20), (50, 20)]:

 CText text1 = canvas.newText(10, 20, "ab");
 CText text2 = canvas.newText(30, 20, "pc");
 canvas.newSegment(10, 20, 50, 20);
 
This can be an issue for setting the position of a CText according to its baseline. Setting the reference point of a CText to its baseline allows to overcome this problem:
 text1.setReferencePointToBaseline().translateTo(10, 20);
 text2.setReferencePointToBaseline().translateTo(30, 20);
 
text1 and text2 appear as if they were written on the same virtual line positioned at y=20.

Returns:
this CText.

setText

public CText setText(java.lang.String text)
Sets the text to be displayed.

Parameters:
text - The text to set.
Returns:
this shape.

getFont

public java.awt.Font getFont()
Returns the text font.

Returns:
the text font.

setFont

public CText setFont(java.awt.Font f)
Sets the text font.

Parameters:
f - The font.
Returns:
this shape.

copyTo

public CShape copyTo(CShape sms)
Copies this shape into a destination shape.

Overrides:
copyTo in class CShape
Parameters:
sms - The destination shape
Returns:
this shape
See Also:
CShape.copyTo(fr.lri.swingstates.canvas.CShape)

duplicate

public CShape duplicate()
Creates a new copy of this shape and returns it.

Overrides:
duplicate in class CShape
Returns:
the copy.
See Also:
CShape.duplicateWithTags()

setAntialiased

public CElement setAntialiased(boolean a)
Specifies whether this shape is antialiased. Antialiasing improves the visual quality of the displayed shapes, at the expense of longer display times.

Specified by:
setAntialiased in interface CElement
Overrides:
setAntialiased in class CShape
Parameters:
a - True if this shape is to be antialiased.
Returns:
this shape.
See Also:
CShape.setAntialiased(boolean)