Package edu.macalester.graphics
Class GraphicsText
java.lang.Object
edu.macalester.graphics.GraphicsObject
edu.macalester.graphics.GraphicsText
A string of text that can be drawn to the screen.
A GraphicsText’s position
is the beginning of the first character at the
baseline. This means that text
largely extends above the position you specify with GraphicsObject.setPosition(edu.macalester.graphics.Point)
. To
position text relative to its top edge, use GraphicsObject.getHeight()
. To format many GraphicsText
objects as a larger body of text, use the getAdvance()
and getLineHeight()
methods.
-
Constructor Summary
ConstructorDescriptionCreates an instance with null text at (0,0)GraphicsText
(String text) Creates an instance with the given text at (0,0).GraphicsText
(String text, double x, double y) Creates drawable text at position (x,y) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
changed()
Triggers a notifications to observers that this object's appearance has changed.protected void
For internal use.double
Returns how far after this text any subsequent text should appear.Determines how each line of this text is aligned horizontally relative to the x coordinate ofGraphicsObject.getPosition()
.Returns the bounding box of this graphics object in its local coordinates.protected Object
For internal use only.Returns the color to fillColor that will be used to fill this shape if it is filled.double
Returns the standard spacing between lines of text in the current font, regardless of the height of the actual characters present.Returns the color to strokeColor that will be used to stroke this shape if it is stroked.double
Returns the thickness of the stroke in pixels.getText()
The text that will appear on the screen as graphics.double
Inserts word wraps (soft line breaks) so that no line of text is wider than the given width.boolean
intersects
(GraphicsText other) Returns true if this text visually overlaps the given other text.boolean
isFilled()
Returns true if the interior of this shape will be filled with the fill color.boolean
Returns true if the interior of this shape will be stroked with the stroke color.void
setAlignment
(TextAlignment alignment) void
setFillColor
(Paint textColor) Sets the fill color to the given color.void
setFilled
(boolean filled) Causes the shape to be filled with the fill color when it is drawn.void
Changes the size and style of the font while preserving the font family.void
Deprecated.Use setFont(family, style, size) insteadvoid
Changes the font in which the text is rendered.void
setFontSize
(double size) Changes the size of text displayed, preserving the font family and style.void
setFontStyle
(FontStyle style) Changes the font style of text displayed, preserving the font family and size.void
setStrokeColor
(Paint strokeColor) Sets the stroke color to the given color.void
setStroked
(boolean stroked) Causes the shape to be stroked with the stroke color when it is drawn.void
setStrokeWidth
(double width) Sets how thick the stroke is.void
void
setWrappingWidth
(double wrappingWidth) boolean
testHitInLocalCoordinates
(double x, double y) Tests whether the given point is inside the boundary or interior of this object’s shape.Methods inherited from class edu.macalester.graphics.GraphicsObject
addObserver, equals, getAnchor, getBoundsInParent, getCanvas, getCenter, getElementAt, getElementAtLocalCoordinates, getEmbeddedComponent, getHeight, getPosition, getRotation, getScale, getScaleX, getScaleY, getSize, getSizeInParent, getWidth, getX, getY, hashCode, isInBounds, moveBy, moveBy, removeObserver, renderToBuffer, rotateBy, setAnchor, setAnchor, setCenter, setCenter, setPosition, setPosition, setRotation, setScale, setScale, setScale, setX, setY, testHit
-
Constructor Details
-
GraphicsText
Creates drawable text at position (x,y) -
GraphicsText
Creates an instance with the given text at (0,0). -
GraphicsText
public GraphicsText()Creates an instance with null text at (0,0)
-
-
Method Details
-
drawInLocalCoordinates
Description copied from class:GraphicsObject
For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.- Specified by:
drawInLocalCoordinates
in classGraphicsObject
-
changed
protected void changed()Description copied from class:GraphicsObject
Triggers a notifications to observers that this object's appearance has changed. Subclasses should call this whenever anything changes that would alter this object's appearance.- Overrides:
changed
in classGraphicsObject
-
getText
The text that will appear on the screen as graphics. Supports line breaks encoded with either CR, LF, or CRLF ("\r", "\n", or "\r\n"), regardless of the runtime platform. -
setText
-
setFontSize
public void setFontSize(double size) Changes the size of text displayed, preserving the font family and style. -
setFontStyle
Changes the font style of text displayed, preserving the font family and size. -
setFont
Changes the size and style of the font while preserving the font family. -
setFont
Changes the font in which the text is rendered.- Parameters:
fontFamilies
- A comma-separated list of font family names, in order of preference, e.g. "Helvetica Neue, Helvetica, Arial"
-
setFont
Deprecated.Use setFont(family, style, size) insteadChanges the font in which the text is rendered. -
getAlignment
Determines how each line of this text is aligned horizontally relative to the x coordinate ofGraphicsObject.getPosition()
. The default isTextAlignment.LEFT
.Note that this only affects horizontal position. The vertical position is always such that the baseline of the first line of text is at the position’s y coordinate.
-
setAlignment
-
getWrappingWidth
public double getWrappingWidth()Inserts word wraps (soft line breaks) so that no line of text is wider than the given width. By default, text does not wrap, no matter how long. Note that this does not affect the hard line breaks created by newlines ("\n") in the text. -
setWrappingWidth
public void setWrappingWidth(double wrappingWidth) -
getFillColor
Description copied from interface:Fillable
Returns the color to fillColor that will be used to fill this shape if it is filled. Note that there will still be no fill if isFilled() is false.- Specified by:
getFillColor
in interfaceFillable
-
setFillColor
Description copied from interface:Fillable
Sets the fill color to the given color. Automatically calls setFilled(true).- Specified by:
setFillColor
in interfaceFillable
-
isFilled
public boolean isFilled()Description copied from interface:Fillable
Returns true if the interior of this shape will be filled with the fill color. -
setFilled
public void setFilled(boolean filled) Description copied from interface:Fillable
Causes the shape to be filled with the fill color when it is drawn. -
getStrokeColor
Description copied from interface:Strokable
Returns the color to strokeColor that will be used to stroke this shape if it is stroked. Note that there will still be no stroke if isStroked() is false or the stroke width is zero.- Specified by:
getStrokeColor
in interfaceStrokable
-
setStrokeColor
Description copied from interface:Strokable
Sets the stroke color to the given color. Automatically calls setStroked(true).- Specified by:
setStrokeColor
in interfaceStrokable
-
isStroked
public boolean isStroked()Description copied from interface:Strokable
Returns true if the interior of this shape will be stroked with the stroke color. -
setStroked
public void setStroked(boolean stroked) Description copied from interface:Strokable
Causes the shape to be stroked with the stroke color when it is drawn.- Specified by:
setStroked
in interfaceStrokable
-
getStrokeWidth
public double getStrokeWidth()Description copied from interface:Strokable
Returns the thickness of the stroke in pixels.- Specified by:
getStrokeWidth
in interfaceStrokable
-
setStrokeWidth
public void setStrokeWidth(double width) Description copied from interface:Strokable
Sets how thick the stroke is. Automatically calls setStroked(true).- Specified by:
setStrokeWidth
in interfaceStrokable
-
testHitInLocalCoordinates
public boolean testHitInLocalCoordinates(double x, double y) Description copied from class:GraphicsObject
Tests whether the given point is inside the boundary or interior of this object’s shape. The point is in this object’s local coordinates.- Specified by:
testHitInLocalCoordinates
in classGraphicsObject
-
intersects
Returns true if this text visually overlaps the given other text. This method assumes both are in the some coordinate system; it does not account for them belonging to different GraphicsGroups. -
getAdvance
public double getAdvance()Returns how far after this text any subsequent text should appear. This is distinct from getWidth() because some glyphs may overhang on either the left or the right, overlapping into the neighboring glyphs’ areas. While getWidth() returns the size of the entire bounding box, including such overhang, getAdvance() does not include the overhang. -
getLineHeight
public double getLineHeight()Returns the standard spacing between lines of text in the current font, regardless of the height of the actual characters present. -
getBounds
Description copied from class:GraphicsObject
Returns the bounding box of this graphics object in its local coordinates.- Specified by:
getBounds
in classGraphicsObject
-
getEqualityAttributes
Description copied from class:GraphicsObject
For internal use only. Used to compute equals() and hashCode(). Returns an object whose equals() and hashCode() methods encompass the subclass-specific values that should be used to compute equality for the whole GraphicsObject.- Specified by:
getEqualityAttributes
in classGraphicsObject
-