fang
Class PrettyStringSprite

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

public class PrettyStringSprite
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 fang.resources 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 fang.resources to make sure the target computers are able to properly display the font (since not all computers have the same fonts available by default).

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
PrettyStringSprite(String text)
          makes a Sprite containing a String resized to fit within a square
PrettyStringSprite(String text, boolean aspect)
          makes a Sprite containing a String
PrettyStringSprite(String text, boolean aspect, double baseRotate)
          makes a Sprite containing a String
 
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.
 Box2D getBounds2D()
          gets the smallest rectangle which will surrond the shape
static Font getFont(int fontStyle, String fontFamily)
          makes the font from the font family name
 String getFontFamilyName()
          gets the family name of the font
 double getHeight()
          gets the vertical span in pixels of this StringSprite
 double getLineHeight()
          gets the full height that this StringSprite could have with any given text
 Location2D getLocation()
          returns the position of this StringSprite.
 Shape getRotatedBoundingBox()
          gets a tight rectangle which fits around the text.
 double getRotation()
          gets the rotation of this sprite.
 String getText()
          gets the characters currently in this StringSprite
 double getWidth()
          gets the horizontal span in pixels of this StringSprite
 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
protected  void normalize()
          currently disabled to prevent loosing height information by rescaling
 void rightJustify()
          makes the position represent the right most position of the string
 void setBold(boolean bold)
          sets the thickness of the lettering
 void setFontFamilyName(String style)
          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 setLineHeight(double height)
          sets the height of an individual line in pixels
 void setLocation(double x, double y)
          moves the StringSprite from its current location to a new location
 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
 void translate(double x, double y)
          moves the StringSprite from its current location
 
Methods inherited from class fang.Sprite
addTransformer, advanceTime, bounceOffOf, disableTransformer, drawDebugShapes, enableTransformer, forward, getBlurLength, getColor, getDebug, getDebugBoundingBoxColor, getDefaultColor, getMaxX, getMaxY, getMinX, getMinY, getNormalVector, getNormalVector, getOptimizedForBitmap, getOrientation, getOrientationDegrees, getOrientationRadians, getOrientationRevolutions, getRotationDegrees, getRotationRadians, getRotationRevolutions, getScale, getShape, getSize, getTracker, getUseBoundingBox, getX, getY, hasTracker, hide, intersects, intersects, intersects, intersects, intersects, isDestroyed, isEnabled, isVisible, paint, paintInternal, paintShape, removeAllTrackers, removeFromCanvas, removeTransformer, rotate, rotateDegrees, rotateRadians, rotateRevolutions, scale, setAbsoluteShape, setBlurLength, setColor, setDebug, setDebugBoundingBoxColor, setDebugBrush, setDefaultColor, setEnabled, setLocation, setLocation, setOptimizedForBitmap, setOrientation, setOrientationDegrees, setOrientationRadians, setOrientationRevolutions, setRotation, setRotationDegrees, setRotationRadians, setRotationRevolutions, setShape, setSize, setTracker, setUseBoundingBox, setVisible, setX, setY, show, 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

PrettyStringSprite

public PrettyStringSprite(String text)
makes a Sprite containing a String resized to fit within a square

Parameters:
text - the String for the Sprite to contain

PrettyStringSprite

public PrettyStringSprite(String text,
                          boolean aspect)
makes a Sprite containing a String

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

PrettyStringSprite

public PrettyStringSprite(String text,
                          boolean aspect,
                          double baseRotate)
makes a Sprite containing a String

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
baseRotate - the original orientation
Method Detail

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

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

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

setItalicized

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

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

setBold

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

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

isItalicized

public boolean isItalicized()
determines if the text is slanted

Returns:
true if the text is slanted, false otherwise

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

leftJustify

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


topJustify

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


bottomJustify

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


rightJustify

public void rightJustify()
makes the position represent the right 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.


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

getFontFamilyName

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

Returns:
the family name of the font

setFontFamilyName

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

Parameters:
style - the family name of the font

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

normalize

protected void normalize()
currently disabled to prevent loosing height information by rescaling

Overrides:
normalize in class Sprite

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

translate

public void translate(double x,
                      double y)
moves the StringSprite from its current location

Overrides:
translate in class Sprite
Parameters:
x - the amount to move from the current horizontal position
y - the amount to move from the current vertical position

setLocation

public void setLocation(double x,
                        double y)
moves the StringSprite from its current location to a new location

Overrides:
setLocation in class Sprite
Parameters:
x - the new horizontal position
y - the new vertical position

getLocation

public Location2D getLocation()
returns the position of this StringSprite. The location will correspond to the leftmost, center, or rightmost point depending on the justification.

Overrides:
getLocation in class Sprite
Returns:
the position of the StringSprite

getWidth

public double getWidth()
gets the horizontal span in pixels of this StringSprite

Overrides:
getWidth in class Sprite
Returns:
the horizontal span

getBounds2D

public Box2D getBounds2D()
gets the smallest rectangle which will surrond the shape

Overrides:
getBounds2D in class Sprite
Returns:
the minimal bounding rectangle

getHeight

public double getHeight()
gets the vertical span in pixels of this StringSprite

Overrides:
getHeight in class Sprite
Returns:
the vertical span

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

getFont

public static Font getFont(int fontStyle,
                           String fontFamily)
makes the font from the font family name

Parameters:
fontStyle - the style (PLAIN/BOLD/ITALIC)
fontFamily - the font's family name
Returns:
the font, or the default font if the family name font is not available

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

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)
sets the text of the Sprite

Parameters:
text - the String for the Sprite to contain

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()

getText

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

Returns:
the sequence of characters in the sprite

getRotatedBoundingBox

public Shape getRotatedBoundingBox()
gets a tight rectangle which fits around the text. The rectangle does not have to be aligned along the x-axis and y-axis.

Returns:
the rectangle surrounding the shape