ch.aplu.jgamegrid
Class GGTextField

java.lang.Object
  extended by ch.aplu.jgamegrid.GGTextField

public class GGTextField
extends java.lang.Object

Class to provide a dynamically created line of text shown in a game grid window. Gets its functionality from a TextActor instance (no subclassing of TextActor in order to shield the user from the many Actor class methods.) Whenever the text, fontSize, textColor, bgColor or typeface attribute is altered, the old text actor is removed from the game grid and a new actor is created and added to the game grid on the next show() invocation.


Constructor Summary
GGTextField(GameGrid gg, Location location, boolean enableRefresh)
          Creates a GGTextField with standard attributes and no text.
GGTextField(GameGrid gg, java.lang.String text, Location location, boolean enableRefresh)
          Creates a GGTextField instance with standard attributes.
 
Method Summary
 TextActor getTextActor()
          Returns the current TextActor used for this text field.
 int getTextHeight()
          Returns the height of the text.
 int getTextWidth()
          Returns the total width of the text.
 void hide()
          Hides the text.
 boolean isVisible()
          Returns the visibility of the text.
 void setBgColor(java.awt.Color bgColor)
          Sets the background color attribute.
 void setFont(java.awt.Font font)
          Sets the font attribute.
 void setLocation(Location location)
          Sets the location attribute.
 void setLocationOffset(java.awt.Point locationOffset)
          Sets a pixel offset in x- any y-direction relative to the current location.
 void setText(java.lang.String text)
          Sets the text attribute.
 void setTextColor(java.awt.Color textColor)
          Sets the text color attribute.
 void show()
          Displays the text.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GGTextField

public GGTextField(GameGrid gg,
                   java.lang.String text,
                   Location location,
                   boolean enableRefresh)
Creates a GGTextField instance with standard attributes. Does not yet show the text. The internal text actor is created and added to the game grid at this point, but the text actor is still hidden. The order of visiblity in respect to other actors does not depend on the moment of creation, because the text actor is set on top of other actors when show() is invoked. The text color is black, the background color white/transparent and the font SansSerif, PLAIN, 8 pixels. Automatic refresh can cause flickering, when the text actors are created rapidly. To avoid flickering, let the simulation thread perform the refreshing.

Parameters:
gg - the GameGrid where to add the text actor
location - the location where the the text will be placed (horizontally left aligned, vertically center aligned)
text - the text string to show
enableRefresh - if true, the automatic refresh of the game grid is turned on; otherwise refresh must be called by user code or performed by the simulation thread

GGTextField

public GGTextField(GameGrid gg,
                   Location location,
                   boolean enableRefresh)
Creates a GGTextField with standard attributes and no text.

Parameters:
gg - the GameGrid where to add the text actor
location - the location where the the text will be placed (horizontally left aligned, vertically center aligned)
enableRefresh - if true, the automatic refresh of the game grid is turned on; otherwise refresh must be called by user code or performed by the simulation thread
Method Detail

show

public void show()
Displays the text. If the text was never shown or attributes were modified, creates the internal TextActor instance. Sets the visibility of the TextActor class on top of all other Actor's classes. This may be changed by calling GameGrid.setPaintOrder(). Be aware that the paint order of all other TextActors is affected. Refreshs the game grid, if enableRefresh = true was set.

See Also:
GameGrid.setPaintOrder(Class... classes)

hide

public void hide()
Hides the text. Refreshs the game grid, if enableRefresh = true was set in the constructor.


isVisible

public boolean isVisible()
Returns the visibility of the text.

Returns:
true, if the text is visible; otherwise false

setLocation

public void setLocation(Location location)
Sets the location attribute. Refreshs the game grid, if enableRefresh = true was set in the constructor.

Parameters:
location - the location where the the text will be placed (horizontally left aligned, vertically center aligned)

setText

public void setText(java.lang.String text)
Sets the text attribute. Refreshs the game grid, if enableRefresh = true was set in the constructor.

Parameters:
text - the text string to show

setTextColor

public void setTextColor(java.awt.Color textColor)
Sets the text color attribute. Refreshs the game grid, if enableRefresh = true was set in the constructor.

Parameters:
textColor - the color of the text

setBgColor

public void setBgColor(java.awt.Color bgColor)
Sets the background color attribute. Refreshs the game grid, if enableRefresh = true was set in the constructor.

Parameters:
bgColor - the color of the text background

setFont

public void setFont(java.awt.Font font)
Sets the font attribute. Refreshs the game grid, if enableRefresh = true was set in the constructor.

Parameters:
font - the text font

setLocationOffset

public void setLocationOffset(java.awt.Point locationOffset)
Sets a pixel offset in x- any y-direction relative to the current location. Refreshs the game grid, if enableRefresh = true was set in the constructor. Used to fine tune the displayed text field position in a coarse game grid.

Parameters:
locationOffset - x,y displacement (x to the left, y downwards)

getTextActor

public TextActor getTextActor()
Returns the current TextActor used for this text field. The actor changes when the text, fontSize, textColor, bgColor or typeface attribute is altered and the modified text is shown.

Returns:
the text actor used for this field; null, if the actor has never been shown

getTextWidth

public int getTextWidth()
Returns the total width of the text. Maybe used to align the text.

Returns:
the text width in pixels

getTextHeight

public int getTextHeight()
Returns the height of the text. Maybe used to align the text.

Returns:
the text height in pixels