fang
Class StringSprite

java.lang.Object
  extended by fang.Sprite
      extended by fang.StringSprite

public class StringSprite
extends Sprite

This class converts text into a sprite. The text can contain multiple lines by putting in newlines. Also, different true type fonts may be used. During development, the fonts may be loaded automatically from the system, but when publishing the game, all fonts used should be copied into the tipgame.fonts package and should have the extension .ttf In Windows, fonts can typically be found in C:\WINDOWS\Fonts. The files corresponding to the fonts used can be copied into tipgame.fonts to make sure the target computers are able to properly display the font (since not all computers have the same fonts available by default). This class differs from the PrettyStringSprite in that it is much faster and sometimes does not represent the font as cleanly.

Author:
Jam Jenkins

Field Summary
 
Fields inherited from class fang.Sprite
color, config, DEBUG_BOUNDING_BOX, DEBUG_INTERSECTS, debugBoundingBoxColor, image, optimizeForBitmap, shape, tracker, transform
 
Constructor Summary
StringSprite()
          makes a Sprite containing a String resized so that the height is 1 pixel
StringSprite(double initialScale)
          Create a StringSprite with a height scale set to the given value.
StringSprite(String text)
          makes a Sprite containing a String resized so that the height is 1 pixel
StringSprite(String text, boolean keepAspect)
          makes a Sprite containing a String resized so that the height is 1 pixel
StringSprite(String text, boolean keepAspect, double baseRotate)
          makes a Sprite containing a String resized so that the height is 1 pixel
StringSprite(String text, double initialScale)
          Create a StringSprite with a height scale set to the given value.
 
Method Summary
 void bottomJustify()
          makes the position represent the bottom most position of the string
 void centerJustify()
          makes the position represent the center position of the string.
 boolean getAspect()
          gets whether the dimensions should or should not be manipulated to fill a square
 Box2D getBounds2D()
          gets the bounds of the shape
 Font getFont()
          gets the style of the current text
 String getFontFamilyName()
          gets the family name of the font
 double getHeight()
          gets the height of this Sprite in pixels
 double getLineHeight()
          gets the full height that this StringSprite could have with any given text
 double getLineWidth(int index)
          gets the width of a given line of text in this sprite.
 double getRotation()
          gets the rotation of this sprite.
 double getScale()
          gets the scale in pixels.
 Shape getShape()
          gets the rotated bounds of the shape.
 String getText()
          gets the characters currently in this StringSprite
 double getWidth()
          gets the width of this Sprite in pixels
 boolean isBold()
          determines the thickness of the lettering
 boolean isItalicized()
          determines if the text is slanted
 boolean isUnderlined()
          determines if there is a line at the baseline of the text
 void leftJustify()
          makes the position represent the left most position of the string
 void paint(Graphics2D brush)
          paints the text by transforming the brush and placing the characters from the letterRepository
 void rightJustify()
          makes the position represent the right most position of the string
 void scale(double s)
          scales the StringSprite by a given factor relative to it's current scale.
 void setBold(boolean bold)
          sets the thickness of the lettering
 void setColor(Color color)
          sets the color of this sprite
 void setFont(Font font)
          sets the style of the current text
 void setFontFamilyName(String familyName)
          sets the family name of the font
 void setHeight(double height)
          makes the size of this sprite such that the height of all lines of the text is a given number of pixels high
 void setItalicized(boolean italics)
          sets the slant of the text
 void setKeepAspect(boolean keepAspect)
          sets whether the dimensions should or should not be manipulated to fill a square
 void setLineHeight(double height)
          sets the height of an individual line in pixels
 void setMonospaced(boolean monospaced)
          sets whether this text should be represented with fixed or variable width text.
 void setScale(double scale)
          sets the scale while keeping the location constant.
 void setText(String text)
          sets the text of the Sprite
 void setText(String text, boolean aspect)
          sets the text of the Sprite
 void setText(String text, boolean aspect, boolean underline)
          updates the text
 void setUnderlined(boolean underline)
          sets whether there should or should not be a line at the baseline of the text
 void setWidth(double width)
          makes the size of this sprite such that the width of the text is a given number of pixels
 void topJustify()
          makes the position represent the top most position of the string
 
Methods inherited from class fang.Sprite
addTransformer, advanceTime, bounceOffOf, disableTransformer, drawDebugShapes, enableTransformer, forward, getBlurLength, getColor, getDebug, getDebugBoundingBoxColor, getDefaultColor, getLocation, getMaxX, getMaxY, getMinX, getMinY, getNormalVector, getNormalVector, getOptimizedForBitmap, getOrientation, getOrientationDegrees, getOrientationRadians, getOrientationRevolutions, getRotationDegrees, getRotationRadians, getRotationRevolutions, getSize, getTracker, getUseBoundingBox, getX, getY, hasTracker, hide, intersects, intersects, intersects, intersects, intersects, isDestroyed, isEnabled, isVisible, normalize, paintInternal, paintShape, removeAllTrackers, removeFromCanvas, removeTransformer, rotate, rotateDegrees, rotateRadians, rotateRevolutions, setAbsoluteShape, setBlurLength, setDebug, setDebugBoundingBoxColor, setDebugBrush, setDefaultColor, setEnabled, setLocation, setLocation, setLocation, setOptimizedForBitmap, setOrientation, setOrientationDegrees, setOrientationRadians, setOrientationRevolutions, setRotation, setRotationDegrees, setRotationRadians, setRotationRevolutions, setShape, setSize, setTracker, setUseBoundingBox, setVisible, setX, setY, show, translate, translate, translate, translateX, translateY, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringSprite

public StringSprite()
makes a Sprite containing a String resized so that the height is 1 pixel


StringSprite

public StringSprite(double initialScale)
Create a StringSprite with a height scale set to the given value. Since there is no text, the width scale is unknown (height is how scale is applied to StringSprites)

Parameters:
initialScale - - scale, in screens, for the full height of the StringSprite

StringSprite

public StringSprite(String text)
makes a Sprite containing a String resized so that the height is 1 pixel

Parameters:
text - - the String to display

StringSprite

public StringSprite(String text,
                    boolean keepAspect)
makes a Sprite containing a String resized so that the height is 1 pixel

Parameters:
text - the String to display
keepAspect - true indicates to leave the aspect ratio of the text unchanged while false means to shrink or expand the text to fit into a square

StringSprite

public StringSprite(String text,
                    boolean keepAspect,
                    double baseRotate)
makes a Sprite containing a String resized so that the height is 1 pixel

Parameters:
text - the String to display
keepAspect - true indicates to leave the aspect ratio of the text unchanged while false means to shrink or expand the text to fit into a square
baseRotate - an initial offset for the rotation which does not affect its original orientation

StringSprite

public StringSprite(String text,
                    double initialScale)
Create a StringSprite with a height scale set to the given value. Since there is no text, the width scale is unknown (height is how scale is applied to StringSprites)

Parameters:
text - - the String to display
initialScale - - scale, in screens, for the full height of the StringSprite
Method Detail

bottomJustify

public void bottomJustify()
makes the position represent the bottom most position of the string


centerJustify

public void centerJustify()
makes the position represent the center position of the string. StringSprites are center justified by default.


getAspect

public boolean getAspect()
gets whether the dimensions should or should not be manipulated to fill a square

Returns:
true if the normal dimensions remain unchanged, false means the text is being expanded to fit a square

getBounds2D

public Box2D getBounds2D()
gets the bounds of the shape

Overrides:
getBounds2D in class Sprite
Returns:
the smallest rectangle fitting around the text

getFont

public Font getFont()
gets the style of the current text

Returns:
the font being used to generate the text

getFontFamilyName

public String getFontFamilyName()
gets the family name of the font

Returns:
the family name of the font

getHeight

public double getHeight()
gets the height of this Sprite in pixels

Overrides:
getHeight in class Sprite
Returns:
the vertical span in pixels

getLineHeight

public double getLineHeight()
gets the full height that this StringSprite could have with any given text

Returns:
the maximum height the StringSprite will take for any given sequence of characters

getLineWidth

public double getLineWidth(int index)
gets the width of a given line of text in this sprite. Invalid lines return width 0.

Parameters:
index - the line number with 0 being the first line
Returns:
the horizontal span of the given line in pixels

getRotation

public double getRotation()
gets the rotation of this sprite. The rotation returned is minus the original base rotation

Overrides:
getRotation in class Sprite
Returns:
the positive rotation in radians less than 2*PI and zero or more
See Also:
fang.core.Sprite#getRotation()

getScale

public double getScale()
gets the scale in pixels.

Overrides:
getScale in class Sprite
Returns:
scale the size in pixels

getShape

public Shape getShape()
gets the rotated bounds of the shape. Note: unlike in other Sprites, this does not give the outline of the individual shapes used to make the text.

Overrides:
getShape in class Sprite
Returns:
the smallest rectangle fitting around the text

getText

public String getText()
gets the characters currently in this StringSprite

Returns:
the sequence of characters in the sprite

getWidth

public double getWidth()
gets the width of this Sprite in pixels

Overrides:
getWidth in class Sprite
Returns:
the horizontal span in pixels

isBold

public boolean isBold()
determines the thickness of the lettering

Returns:
true if the text has extra thick lettering, false if the thickness is normal

isItalicized

public boolean isItalicized()
determines if the text is slanted

Returns:
true if the text is slanted, false otherwise

isUnderlined

public boolean isUnderlined()
determines if there is a line at the baseline of the text

Returns:
true if there is a line, false if there is not

leftJustify

public void leftJustify()
makes the position represent the left most position of the string


paint

public void paint(Graphics2D brush)
paints the text by transforming the brush and placing the characters from the letterRepository

Overrides:
paint in class Sprite
Parameters:
brush - the drawing instrument

rightJustify

public void rightJustify()
makes the position represent the right most position of the string


scale

public void scale(double s)
scales the StringSprite by a given factor relative to it's current scale.

Overrides:
scale in class Sprite
Parameters:
s - the scaling factor

setBold

public void setBold(boolean bold)
sets the thickness of the lettering

Parameters:
bold - true indicates thick lettering, false indicates normal thickness

setColor

public void setColor(Color color)
sets the color of this sprite

Overrides:
setColor in class Sprite
Parameters:
color - the shade of the sprite

setFont

public void setFont(Font font)
sets the style of the current text

Parameters:
font - the font being used to generate the text

setFontFamilyName

public void setFontFamilyName(String familyName)
sets the family name of the font

Parameters:
familyName - the family name of the font

setHeight

public void setHeight(double height)
makes the size of this sprite such that the height of all lines of the text is a given number of pixels high

Parameters:
height - the pixels high of the lines of text

setItalicized

public void setItalicized(boolean italics)
sets the slant of the text

Parameters:
italics - true indicates slant, false is for no slant

setKeepAspect

public void setKeepAspect(boolean keepAspect)
sets whether the dimensions should or should not be manipulated to fill a square

Parameters:
keepAspect - true indicates the normal dimensions should remain unchanged, false means the text should be expanded to fit a square

setLineHeight

public void setLineHeight(double height)
sets the height of an individual line in pixels

Parameters:
height - the height of one line of text in pixels

setMonospaced

public void setMonospaced(boolean monospaced)
sets whether this text should be represented with fixed or variable width text.

Parameters:
monospaced - true indicates fixed width text, false means variable width text

setScale

public void setScale(double scale)
sets the scale while keeping the location constant. This is necessary because of the justification manipulations.

Overrides:
setScale in class Sprite
Parameters:
scale - the size in pixels

setText

public void setText(String text)
sets the text of the Sprite

Parameters:
text - the String for the Sprite to contain

setText

public void setText(String text,
                    boolean aspect)
sets the text of the Sprite

Parameters:
text - the String for the Sprite to contain
aspect - true indicates original width and height should be retained, false indicates the text should be resized to fit within a square

setText

public void setText(String text,
                    boolean aspect,
                    boolean underline)
updates the text

Parameters:
text - the new text
aspect - false means the text should be expanded to fit a square, true indicates the text should retain its original shape
underline - true indicates there should be a line at the baseline, false indicates no line

setUnderlined

public void setUnderlined(boolean underline)
sets whether there should or should not be a line at the baseline of the text

Parameters:
underline - true indicates there should be a line, false indicates no line should be there

setWidth

public void setWidth(double width)
makes the size of this sprite such that the width of the text is a given number of pixels

Parameters:
width - the pixels wide of the original text

topJustify

public void topJustify()
makes the position represent the top most position of the string