Class Text
- java.lang.Object
-
- paintingcanvas.drawable.DrawableBase<Text>
-
- paintingcanvas.drawable.Text
-
- All Implemented Interfaces:
Animatable,Anchorable<Text>,Colorable<Text>,Drawable<Text>,Positionable<Text>
public class Text extends DrawableBase<Text> implements Anchorable<Text>
A Text element, used for drawing text on the canvas.
Anchors work a bit different for text elements.
Anchor.CENTERcenters the text at the baseline. This requiresAnchor.TOP_CENTERto differ by the ascent of the text, andAnchor.BOTTOM_CENTERto differ by the descent.Text text = new Text(100, 100, "Hello World");
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class paintingcanvas.drawable.DrawableBase
DrawableBase.InteractableShape<T extends Drawable<T>>, DrawableBase.OutlineableDrawableBase<T extends Drawable<T>>, DrawableBase.Shape<T extends Drawable<T>>
-
-
Constructor Summary
Constructors Constructor Description Text(int x, int y, java.lang.String text)Create a new Text element.Text(int x, int y, java.lang.String text, java.awt.Color color)Create a new Text element.Text(int x, int y, java.lang.String text, java.lang.String color)Create a new Text element with a hue name or hex code
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.Pointcenter(java.awt.Graphics2D g)Get the object's centerpointAnchorgetAnchor()Get the current anchor of the element.java.awt.FontMetricsgetFontMetrics()If you need more information about the text, such as ascent, descent, leading, etc.intgetFontSize()Get the current font size of the text.intgetHeight()Note that the computed height of the text only gets updated when the text is drawn.java.lang.StringgetText()Gets the current text of the element.TextgetThis()intgetWidth()Note that the computed width of the text only gets updated when the text is drawn.voidinternalSetAnchor(Anchor anchor)TextsetFontSize(double size)Set the current font size of the text.TextsetText(java.lang.String text)Sets the text of the element.-
Methods inherited from class paintingcanvas.drawable.DrawableBase
Modifier and Type Method Description java.awt.ColorgetColor()Get the current color of an element as aColorintgetLayer()Gets the current layer of the object.java.awt.PointgetPos()Get the position of the elementdoublegetRotation()Get an elements rotationThide()Hide the Object.doubleinternalGetRotation()voidinternalSetColor(java.awt.Color color)voidinternalSetLayer(int layer)voidinternalSetPos(int x, int y)voidinternalSetRotation(double rotation)voidrender(java.awt.Graphics2D g)Actually render the object itselfTshow()Show the Object
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface paintingcanvas.drawable.Anchorable
Modifier and Type Method Description AnchorgetAnchor()Get the current anchor of the element.voidinternalSetAnchor(Anchor anchor)default TsetAnchor(Anchor anchor)Set the anchor of the element.
-
Methods inherited from interface paintingcanvas.animation.Animatable
Modifier and Type Method Description default AnimationBuildercolorTo(int hex, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.default AnimationBuildercolorTo(int r, int g, int b, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.default AnimationBuildercolorTo(int r, int g, int b, int a, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.default AnimationBuildercolorTo(java.awt.Color color, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.default AnimationBuildercolorTo(java.lang.String name, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.default AnimationBuildercolorTo(Hue hue, double duration)Change the color ofthisto the specifiedcoloroverdurationseconds.Drawable<?>drawable()Get theDrawableelement from thisAnimatable.default AnimationBuilderfadeIn(double duration)Fadethisin overdurationseconds.default AnimationBuilderfadeOut(double duration)Fadethisout overdurationseconds.default AnimationBuildermoveBy(int x, int y, double duration)Movethisby the specifiedxandyoverdurationseconds.default AnimationBuildermoveHorizontalBy(int x, double duration)Movethisby the specifiedxhorizontally overdurationseconds.default AnimationBuildermoveTo(int x, int y, double duration)Movethisto the specifiedxandyoverdurationsecondsdefault AnimationBuildermoveVerticalBy(int y, double duration)Movethisby the specifiedyvertically overdurationseconds.default AnimationBuilderrotateBy(int angle, double duration)Rotatethisbyangledegrees overdurationseconds.default AnimationBuilderrotateTo(int angle, double duration)Rotatethisto the specifiedangledegrees overdurationseconds.
-
Methods inherited from interface paintingcanvas.drawable.Colorable
Modifier and Type Method Description java.awt.ColorgetColor()Get the current color of an element as aColorvoidinternalSetColor(java.awt.Color color)default TsetColor(int hex)Set the color ofthisto the specifiedcolor.default TsetColor(int r, int g, int b)Set the color ofthisto the specifiedcolor.default TsetColor(int r, int g, int b, int a)default TsetColor(java.awt.Color color)Set the color of the object with aColorobject.default TsetColor(java.lang.String name)Set the color of the object with a hue name or hex code.default TsetColor(Hue hue)Set the color of the object with aHueobject.
-
Methods inherited from interface paintingcanvas.drawable.Drawable
Modifier and Type Method Description default AnimationBuilderanimate()Start animating this object.default TbringToFront()Brings the object in front of all other objects.java.awt.Pointcenter(java.awt.Graphics2D g)Get the object's centerpointdefault Drawable<?>drawable()Get theDrawableelement from thisAnimatable.default voiderase()Erase this object from the canvas.intgetLayer()Gets the current layer of the object.TgetThis()Thide()Hide the Object.voidinternalSetLayer(int layer)voidrender(java.awt.Graphics2D g)Actually render the object itselfdefault TsendToBack()Puts the object behind all other objects.default TsetLayer(int layer)Puts the object on a specific layer.Tshow()Show the Object
-
Methods inherited from interface paintingcanvas.drawable.Positionable
Modifier and Type Method Description java.awt.PointgetPos()Get the position of the elementdoublegetRotation()Get an elements rotationdefault intgetX()Get the X-position of the elementdefault intgetY()Get the Y-position of the element.doubleinternalGetRotation()voidinternalSetPos(int x, int y)voidinternalSetRotation(double rotation)default Tmove(int x, int y)Moves this drawable by the specified x and y.default TmoveHorizontal(int x)Moves this drawable by the specified x and y.default TmoveVertical(int y)Moves this drawable by the specified x and y.default Trotate(double rotation)Rotate this element byrotationdegrees.default TsetPos(int x, int y)Set the position of the element.default TsetRotation(double rotation)Set an elements rotation torotationdegrees.default TsetX(int x)Set the X-position of the objectdefault TsetY(int y)Set the Y-position of the element
-
-
-
-
Constructor Detail
-
Text
public Text(int x, int y, java.lang.String text)Create a new Text element. The default font size is 30, and the default font is comic sans :)
It probably won't work on replit however. Comic sans is not installed on the replit servers by default.
Text text = new Text(100, 100, "Hello World")- Parameters:
x- The X-position of the texty- The Y-position of the texttext- The text
-
Text
public Text(int x, int y, java.lang.String text, java.awt.Color color)Create a new Text element. The default font size is 30, and the default font is comic sans :)
It probably won't work on replit however. Comic sans is not installed on the replit servers by default.
Text text = new Text(100, 100, "Hello World", new Color(255, 0, 0))- Parameters:
x- The X-position of the texty- The Y-position of the texttext- The textcolor- the color of the text
-
Text
public Text(int x, int y, java.lang.String text, java.lang.String color)Create a new Text element with a hue name or hex code
- Parameters:
x- The X-position of the texty- The Y-position of the texttext- The textcolor- The name of the color of the text (case-insensitive)- See Also:
The default font size is 30, and the default font is comic sans :) It probably won't work on replit however. Comic sans is not installed on the replit servers by default.
-
-
Method Detail
-
center
public java.awt.Point center(java.awt.Graphics2D g)
Description copied from interface:DrawableGet the object's centerpointimport java.awt.Point; Point p = drawable.getStartpoint(); int x = p.x; int y = p.y;
-
getWidth
public int getWidth()
Note that the computed width of the text only gets updated when the text is drawn. (i.e. it does not get updated immediately)
- Returns:
- The width of the text in pixels.
-
getHeight
public int getHeight()
Note that the computed height of the text only gets updated when the text is drawn. (i.e. it does not get updated immediately)
- Returns:
- The height of the text in pixels.
-
getFontMetrics
public java.awt.FontMetrics getFontMetrics()
If you need more information about the text, such as ascent, descent, leading, etc. the information can be retrieved from theFontMetricsobject.Again, note that the metrics are only updated when the text is drawn. (i.e. it does not get updated immediately)
- Returns:
- The FontMetrics object containing information about the text
-
getFontSize
public int getFontSize()
Get the current font size of the text.Text text = new Text(100, 100, "Hello World"); text.setFontSize(40); // Set font size to 40 points System.out.println(text.getFontSize()); // Prints 40- Returns:
- The font size in points
- See Also:
setFontSize(double)
-
setFontSize
public Text setFontSize(double size)
Set the current font size of the text.Text text = new Text(100, 100, "Hello World"); text.setFontSize(40); // Set font size to 40 points System.out.println(text.getFontSize()); // Prints 40- Parameters:
size- the new font size of the text- Returns:
- The original object to allow method chaining
- See Also:
getFontSize()
-
getText
public java.lang.String getText()
Gets the current text of the element.Text text = new Text(100, 100, "Hello World"); System.out.println(text.getText()); // => Hello World- Returns:
- The text as a
String - See Also:
setText(String)
-
setText
public Text setText(java.lang.String text)
Sets the text of the element.Text text = new Text(100, 100, "Hello World"); text.setText("Go Go Mango");- Parameters:
text- The new text for the element as aString- Returns:
- The original object to allow method chaining
-
getAnchor
public Anchor getAnchor()
Description copied from interface:AnchorableGet the current anchor of the element.- Specified by:
getAnchorin interfaceAnchorable<Text>- Returns:
- the
Anchorof the element; how its positioned relative to its center
-
internalSetAnchor
public void internalSetAnchor(Anchor anchor)
- Specified by:
internalSetAnchorin interfaceAnchorable<Text>
-
-