public class TextDoodle extends FlatDoodle implements LocationDoodle, Cloneable
Details:
A TextDoodle
is a Doodle
that displays a string of text.
In many ways, a TextDoodle
is like a label for your figure.
However, just like any other Doodle
, a TextDoodle
is scaled, translated, and rotated with the figure.
TextDoodle
allows you to position the text in the figure relative to the beginning of the text's baseline or to any other point relative to the baseline.
The properties that provide control over text positioning are:
Example: To center the text "Hello!" on the point (100, 100) in the figure, try the following:
TextDoodle text = new TextDoodle(); text.setText("Hello!"); Rectangle2D bounds = text.getBounds(null); text.setOrigin ( new Point2D.Double ( bounds.getCenterX(), bounds.getCenterY() ) ); text.setLocation(new Point(100, 100));
This code defines, for positioning purposes, the text's location to be wherever the text's center is. Then, it positions the text at the target location. This puts the text's center at the target location.
The example above positions the text relative to its bounding box -- in this case, the box's center.
An easier way to set the text's origin is to use the setAlignment(Point2D)
method, as follows:
TextDoodle text = new TextDoodle(); text.setText("Hello!"); text.setAlignment(new Point2D.Float(0.5f, 0.5f)); text.setLocation(new Point(100, 100));
For more information about this technique, see mOriginOrAlignment
.
Modifier and Type | Field and Description |
---|---|
protected boolean |
mAligned
Indicates whether alignment is active.
|
protected double |
mAngleRadians
Rotation angle.
|
protected Font |
mFont
Font.
|
protected Point2D |
mLocation
Location of text.
|
protected Point2D |
mOriginOrAlignment
Text origin.
|
protected Paint |
mPaint
Paint used to draw text.
|
protected String |
mText
Displayed text.
|
mToolTip, mZOrder
mParent
Constructor and Description |
---|
TextDoodle() |
Modifier and Type | Method and Description |
---|---|
boolean |
contains(AffineTransform iTransform,
Point2D iPoint)
Tests point for containment.
|
boolean |
draw(FigGraphics iG)
Renders doodle.
|
Point2D |
getAlignment()
Returns current alignment.
|
double |
getAngleDegrees()
Returns rotation angle in degrees.
|
double |
getAngleRadians()
Returns rotation angle in radians.
|
protected Rectangle2D |
getBounds() |
Rectangle2D |
getBounds(AffineTransform iTransform,
Rectangle2D oBounds)
Returns rectangular extent.
|
Point2D |
getDefaultOrigin() |
Font |
getFont()
Returns font.
|
Point2D |
getLocation()
Returns location.
|
Point2D |
getOrigin()
Returns effective origin.
|
protected Shape |
getOutline() |
Paint |
getPaint()
Returns paint.
|
String |
getText()
Returns text.
|
protected AffineTransform |
getTransform() |
void |
setAlignment(Point2D iAlignment)
Sets alignment.
|
void |
setAngleDegrees(double iAngleDegrees)
Sets rotation angle in degrees.
|
void |
setAngleRadians(double iAngle)
Sets rotation angle, in radians.
|
void |
setFont(Font iFont)
Sets font.
|
void |
setLocation(Point2D iLocation)
Details:
This method updates the Location property.
|
void |
setOrigin(Point2D iOrigin)
Sets origin.
|
void |
setPaint(Paint iPaint)
Sets paint.
|
void |
setText(String iText)
Sets text.
|
getToolTip, getZOrder, setToolTip, setZOrder
getParent, redraw, setParent
protected String mText
Details:
The Text property is the text displayed by this TextDoodle
.
It is never null
but may be an empty string.
This field is the value of the Text property.
getText()
,
setText(String)
protected Font mFont
Details:
The Font property is the Font
used to draw this TextDoodle
's text.
It cannot be null
.
The default value of this property is the system default font at 12 points.
getFont()
,
setFont(Font)
protected Paint mPaint
Details:
The Paint property is the Paint
used to draw this TextDoodle
.
This property can never be null
.
Color
is a kind of Paint
.
To draw text in a specific color, simply assigned the desired Color
to the Paint property.
The default value for the Paint property is Color.black
.
This field is the value of the Paint property.
getPaint()
,
setPaint(Paint)
protected Point2D mOriginOrAlignment
Details: The Origin property is the coordinate, relative to the beginning of the text's baseline, where the text is said to be located at. In other words, when the text is positioned at a certain point, it is the text's origin that is actually located at that point. The origin is also the point around which the text is rotated, when rotation is specified. The default value of Origin is (0, 0), or the beginning of the text's baseline.
The Alignment property is the same concept as the Origin property, except that it uses a different coordinate system. In Alignment's coordinate system, the positive unit square coincides with the text's bounding rectangle in Origin's coordinate system. This means that Alignment's coordinate system is asymmetrically scaled in most cases, so that:Setting the text's origin through the Alignment property makes it easy to line up text with other objects. For example, to place all text to the left of some point P, one only needs to specify an alignment coordinate with y = 1, and then set the text's location to P. Alignment coordinates are rarely formed from scalars other than 0.0, 0.5, and 1.0, but it is not illegal to do so. Note that the bounding box includes descenders. When the text does not include descenders, the bottom edge of the baseline will be very similar to the text's baseline.
Alignment is either "active" or "inactive."
Alignment is activated by setting the Alignment property.
It is deactivated by setting the Origin property.
The two properties cannot be simultaneously defined.
To determine which mode is active, use getAlignment()
.
protected boolean mAligned
Details:
This field indicates whether text alignment is active.
Alignment is activated when setAlignment(Point2D)
is called.
It is cancelled when setOrigin(Point2D)
is called.
mOriginOrAlignment
protected Point2D mLocation
Details:
The Location property is the location of the text, in figure coordinates.
The text is positioned so that the text's origin is at this location.
This property is never null
.
getLocation()
,
setLocation(Point2D)
protected double mAngleRadians
Details: The Angle property indicates the rotation of the text about its effective origin.
This field is the value of the Angle property, in radians.
public String getText()
Details: This method returns the value of the Text property.
mText
,
setText(String)
public void setText(String iText)
Details:
This method updates the Text property to the given value.
The new value cannot be null
.
To display no text, set the text to an empty string.
public Font getFont()
Details: This method returns the value of the Font property.
mFont
,
setFont(Font)
public void setFont(Font iFont)
Details:
This method updates the value of the Font property.
The new font may not be null
.
public Paint getPaint()
Details: This method returns the value of the Paint property.
mPaint
,
setPaint(Paint)
public void setPaint(Paint iPaint)
Details: This method sets the Paint property.
iPaint
- the paintpublic Point2D getOrigin()
Details: This method returns this text's effective origin. If alignment is not active, the effective origin is simply the value of the Origin property. Otherwise, the effective origin is computed from Alignment.
getOrigin
in interface LocationDoodle
mOriginOrAlignment
,
setOrigin(Point2D)
public void setOrigin(Point2D iOrigin)
Details: This method updates the Origin property. If alignment is active, it is cancelled.
setOrigin
in interface LocationDoodle
iOrigin
- the new originmOriginOrAlignment
,
getOrigin()
public Point2D getAlignment()
Details:
This method returns the current value of the Alignment property.
If this value is null
, it indicates that alignment is not active.
mOriginOrAlignment
,
setAlignment(Point2D)
public void setAlignment(Point2D iAlignment)
Details:
This method updates the Alignment property to the given value.
If the new value is null
, it cancels alignment and sets the Origin
property to the current effective origin.
iAlignment
- the new alignmentmOriginOrAlignment
,
getOrigin()
,
getAlignment()
public Point2D getLocation()
Details: This method returns the value of the Location property.
getLocation
in interface LocationDoodle
mLocation
,
setLocation(Point2D)
public void setLocation(Point2D iLocation)
Details:
This method updates the Location property.
This effectively moves the text to the given location.
If the new location is null
, the text is positioned at the figure's origin.
setLocation
in interface LocationDoodle
iLocation
- the locationmLocation
,
getLocation()
public double getAngleRadians()
Details: This method returns the value of the Angle property, in radians.
mAngleRadians
,
setAngleRadians(double)
,
getAngleDegrees()
public void setAngleRadians(double iAngle)
Details: This method updates the Angle property with the specified value, given in radians.
iAngle
- the new rotation anglemAngleRadians
,
getAngleRadians()
,
setAngleDegrees(double)
public double getAngleDegrees()
Details: This method returns the value of the Angle property, in degrees.
Subclass consideration:
This method makes a call to getAngleRadians()
, converts the angle, and returns the result.
public void setAngleDegrees(double iAngleDegrees)
Details: This method updates the Angle property with the specified value, given in degrees.
Subclass consideration:
This method converts the angle to radians and calls setAngleRadians(double)
.
iAngleDegrees
- the angleprotected AffineTransform getTransform()
public boolean draw(FigGraphics iG)
Doodle
Details:
Doodle.draw(FigGraphics)
renders this doodle using the supplied graphics environment.
When the host FigureView
is repainting itself, it will probably call this method, albeit indirectly, to redraw its figure's components.
This method returns true
if drawing was successful, false
otherwise.
protected Shape getOutline()
protected Rectangle2D getBounds()
public Rectangle2D getBounds(AffineTransform iTransform, Rectangle2D oBounds)
Doodle
Details:
This method returns the rectangular extent of this Doodle
.
The rectangle returned is in logical (i.e. figure) units, not display units.
A provided AffineTransform
reveals the characteristics of the viewing context in which the bounds will be considered.
This information is meaningful only in Doodle
s that alter their logical form to accommodate the view, such as ViewDoodle
.
Most implementations will simply ignore this transform, and in some cases, this transform may be null
, indicating that the transform is a "don't-
Optionally, the caller may pass in a pre-null
, a new rectangle will be instantiated, initialized with the result, and returned.
In either case, the rectangle containing the result is returned.
public boolean contains(AffineTransform iTransform, Point2D iPoint)
Details:
contains
tests the given point to see whether it is contained in this TextDoodle
.
If it is, this method returns true
, false
otherwise.
public Point2D getDefaultOrigin()
getDefaultOrigin
in interface DefaultOriginDoodle
getDefaultOrigin
in class ChildDoodle
Copyright © 2013. All Rights Reserved.