NASA World Wind

gov.nasa.worldwind.render
Class SurfaceText

java.lang.Object
  extended by gov.nasa.worldwind.avlist.AVListImpl
      extended by gov.nasa.worldwind.WWObjectImpl
          extended by gov.nasa.worldwind.render.AbstractSurfaceObject
              extended by gov.nasa.worldwind.render.SurfaceText
All Implemented Interfaces:
AVList, MessageListener, Movable, GeographicText, OrderedRenderable, PreRenderable, Renderable, SurfaceObject, WWObject, PropertyChangeListener, EventListener

public class SurfaceText
extends AbstractSurfaceObject
implements GeographicText, Movable

Renders a string of text on the surface of the globe. The text will appear draped over terrain. Surface text is drawn at a constant geographic size: it will appear larger when the view zooms in on the text and smaller when the view zooms out.


Nested Class Summary
 
Nested classes/interfaces inherited from class gov.nasa.worldwind.render.AbstractSurfaceObject
AbstractSurfaceObject.CacheEntry, AbstractSurfaceObject.SurfaceObjectStateKey
 
Field Summary
protected  Color bgColor
          Background color for the text.
protected  Color color
          Color to use to draw the text.
static Color DEFAULT_COLOR
          Default text color.
static Font DEFAULT_FONT
          Default font.
static Offset DEFAULT_OFFSET
          Default offset.
static double DEFAULT_TEXT_SIZE_IN_METERS
          Default text size.
protected  Font font
          Font to use to draw the text.
protected  Position location
          Location at which to draw the text.
protected  Offset offset
          Offset that specifies where to place the text in relation to it's geographic position.
protected  double pixelSizeInMeters
          Geographic size of a pixel.
protected  double priority
          Text priority.
protected  double scale
          Scaling factor applied to the text to maintain a constant geographic size.
protected  CharSequence text
          The text to draw.
protected  Rectangle2D textBounds
          Bounds of the text in pixels.
protected  double textSizeInMeters
          The height of the text in meters.
 
Fields inherited from class gov.nasa.worldwind.render.AbstractSurfaceObject
delegateOwner, drawBoundingSectors, enableBatchPicking, extentCache, lastModifiedTime, nextUniqueId, pickLayer, pickSupport, pickTileBuilder, pickTiles, uniqueId, visible
 
Constructor Summary
SurfaceText(String text, Position position)
          Create a new surface text object.
SurfaceText(String text, Position position, Font font, Color color)
          Create a new surface text object.
 
Method Summary
protected  void applyDrawTransform(DrawContext dc, SurfaceTileDrawContext sdc)
          Apply a transform to the GL state to draw the text at the proper location and scale.
protected  Color computeBackgroundColor(Color color)
          Compute a background color that contrasts with the text color.
protected  void computeGeometry(DrawContext dc, SurfaceTileDrawContext sdc)
          Compute the text size and position.
protected  double computePixelSize(DrawContext dc, SurfaceTileDrawContext sdc)
          Compute the size of a pixel in the surface tile.
protected  Sector computeSector(DrawContext dc)
          Compute the sector covered by this surface text.
protected  Color determineBackgroundColor(Color color)
          Determine the text background color.
protected  void drawGeographic(DrawContext dc, SurfaceTileDrawContext sdc)
          Causes the SurfaceObject to render itself to the specified region in geographic coordinates.
protected  void drawText(DrawContext dc)
          Draw the text.
 Color getBackgroundColor()
          Indicates the background color used to draw the text.
 Color getColor()
          Indicates the color used to draw the text.
 Font getFont()
          Indicates the font used to draw the text.
 Offset getOffset()
          Returns the text offset.
 Position getPosition()
          Indicates the geographic position of the text.
 double getPriority()
          Indicates the text priority.
 Position getReferencePosition()
          A position associated with the object that indicates its aggregate geographic position.
 List<Sector> getSectors(DrawContext dc)
          Returns a List of Sector instances that bound the surface object on the specified DrawContext.
 CharSequence getText()
          Indicates the text contained in this object.
protected  TextRenderer getTextRenderer(DrawContext dc)
          Get the text renderer to use to draw text.
 double getTextSize()
           
protected  boolean isSmall()
          Determine if the text is too small to draw.
 void move(Position position)
          Shift the shape over the globe's surface while maintaining its original azimuth, its orientation relative to North.
 void moveTo(Position position)
          Move the shape over the globe's surface while maintaining its original azimuth, its orientation relative to North.
 void preRender(DrawContext dc)
          Causes the surface object to prepare a representation of itself which can be drawn on the surface terrain, using the provided draw context.
 void setBackgroundColor(Color background)
          Specifies the background color used to draw the text.
 void setColor(Color color)
          Specifies the color used to draw the text.
 void setFont(Font font)
          Specifies the font used to draw the text.
 void setOffset(Offset offset)
          Specifies a location relative to the label position at which to align the label.
 void setPosition(Position position)
          Specifies the geographic position of the text.
 void setPriority(double priority)
          Specifies the text priority.
 void setText(CharSequence text)
          Specifies the text.
 void setTextSize(double meters)
           
protected  void updateTextBounds(DrawContext dc)
          Determine the text bounds.
 
Methods inherited from class gov.nasa.worldwind.render.AbstractSurfaceObject
buildPickRepresentation, clearCaches, computeExtent, computeExtent, createPickedObject, createPickTileBuilder, drawBoundingSectors, drawOrderedRenderable, drawPickRepresentation, getDelegateOwner, getDistanceFromEye, getExtent, getStateKey, getUniqueId, intersectsFrustum, intersectsPickFrustum, intersectsVisibleSector, isDrawBoundingSectors, isEnableBatchPicking, isVisible, makeOrderedPreRenderable, makeOrderedRenderable, nextUniqueId, pick, pickBatched, pickOrderedRenderable, render, setDelegateOwner, setDrawBoundingSectors, setEnableBatchPicking, setVisible, updateModifiedTime
 
Methods inherited from class gov.nasa.worldwind.WWObjectImpl
onMessage, propertyChange
 
Methods inherited from class gov.nasa.worldwind.avlist.AVListImpl
addPropertyChangeListener, addPropertyChangeListener, clearList, copy, firePropertyChange, firePropertyChange, getBooleanValue, getBooleanValue, getChangeSupport, getDoubleValue, getDoubleValue, getEntries, getIntegerValue, getIntegerValue, getLongValue, getLongValue, getRestorableStateForAVPair, getStringValue, getStringValue, getStringValue, getValue, getValues, hasKey, removeKey, removePropertyChangeListener, removePropertyChangeListener, setValue, setValues
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gov.nasa.worldwind.render.GeographicText
isVisible, setVisible
 
Methods inherited from interface gov.nasa.worldwind.avlist.AVList
addPropertyChangeListener, addPropertyChangeListener, clearList, copy, firePropertyChange, firePropertyChange, getEntries, getStringValue, getValue, getValues, hasKey, removeKey, removePropertyChangeListener, removePropertyChangeListener, setValue, setValues
 

Field Detail

bgColor

protected Color bgColor
Background color for the text. By default color will be generated to contrast with the text color.


color

protected Color color
Color to use to draw the text. Defaults to DEFAULT_COLOR.


DEFAULT_COLOR

public static final Color DEFAULT_COLOR
Default text color.


DEFAULT_FONT

public static final Font DEFAULT_FONT
Default font.


DEFAULT_OFFSET

public static final Offset DEFAULT_OFFSET
Default offset. The default offset centers the text on its geographic position both horizontally and vertically.


DEFAULT_TEXT_SIZE_IN_METERS

public static final double DEFAULT_TEXT_SIZE_IN_METERS
Default text size.

See Also:
Constant Field Values

font

protected Font font
Font to use to draw the text. Defaults to DEFAULT_FONT.


location

protected Position location
Location at which to draw the text.


offset

protected Offset offset
Offset that specifies where to place the text in relation to it's geographic position.


pixelSizeInMeters

protected double pixelSizeInMeters
Geographic size of a pixel.


priority

protected double priority
Text priority. Can be used to implement text culling.


scale

protected double scale
Scaling factor applied to the text to maintain a constant geographic size.


text

protected CharSequence text
The text to draw.


textBounds

protected Rectangle2D textBounds
Bounds of the text in pixels.


textSizeInMeters

protected double textSizeInMeters
The height of the text in meters.

Constructor Detail

SurfaceText

public SurfaceText(String text,
                   Position position)
Create a new surface text object.

Parameters:
text - Text to draw.
position - Geographic location at which to draw the text.

SurfaceText

public SurfaceText(String text,
                   Position position,
                   Font font,
                   Color color)
Create a new surface text object.

Parameters:
text - Text to draw.
position - Geographic location at which to draw the text.
font - Font to use when drawing text.
color - Color to use when drawing text.
Method Detail

applyDrawTransform

protected void applyDrawTransform(DrawContext dc,
                                  SurfaceTileDrawContext sdc)
Apply a transform to the GL state to draw the text at the proper location and scale.

Parameters:
dc - Current draw context.
sdc - Current surface tile draw context.

computeBackgroundColor

protected Color computeBackgroundColor(Color color)
Compute a background color that contrasts with the text color.

Parameters:
color - text color.
Returns:
a color that contrasts with the text color.

computeGeometry

protected void computeGeometry(DrawContext dc,
                               SurfaceTileDrawContext sdc)
Compute the text size and position.

Parameters:
dc - Current draw context.
sdc - Current surface tile draw context.

computePixelSize

protected double computePixelSize(DrawContext dc,
                                  SurfaceTileDrawContext sdc)
Compute the size of a pixel in the surface tile.

Parameters:
dc - Current draw context.
sdc - Current surface tile draw context.
Returns:
The size of a tile pixel in meters.

computeSector

protected Sector computeSector(DrawContext dc)
Compute the sector covered by this surface text.

Parameters:
dc - Current draw context.
Returns:
The sector covered by the surface text.

determineBackgroundColor

protected Color determineBackgroundColor(Color color)
Determine the text background color. This method returns the user specified background color, or a computed default color if the user has not set a background color.

Parameters:
color - text color.
Returns:
the user specified background color, or a default color that contrasts with the text color.

drawGeographic

protected void drawGeographic(DrawContext dc,
                              SurfaceTileDrawContext sdc)
Causes the SurfaceObject to render itself to the specified region in geographic coordinates. The specified viewport denotes the geographic region and its corresponding screen viewport.

Specified by:
drawGeographic in class AbstractSurfaceObject
Parameters:
dc - the current draw context.
sdc - the context containing a geographic region and screen viewport corresponding to a surface tile.

drawText

protected void drawText(DrawContext dc)
Draw the text.

Parameters:
dc - Current draw context.

getBackgroundColor

public Color getBackgroundColor()
Indicates the background color used to draw the text.

Specified by:
getBackgroundColor in interface GeographicText
Returns:
Current background color.

getColor

public Color getColor()
Indicates the color used to draw the text.

Specified by:
getColor in interface GeographicText
Returns:
Current text color.

getFont

public Font getFont()
Indicates the font used to draw the text.

Specified by:
getFont in interface GeographicText
Returns:
Current font.

getOffset

public Offset getOffset()
Returns the text offset. The offset determines how to position the text relative to its geographic position.

Returns:
the text offset.
See Also:
setOffset(Offset)

getPosition

public Position getPosition()
Indicates the geographic position of the text.

Specified by:
getPosition in interface GeographicText
Returns:
The text position.

getPriority

public double getPriority()
Indicates the text priority. The priority can be used to implement text culling.

Specified by:
getPriority in interface GeographicText
Returns:
The text priority.

getReferencePosition

public Position getReferencePosition()
A position associated with the object that indicates its aggregate geographic position. The chosen position varies among implementers of this interface. For objects defined by a list of positions, the reference position is typically the first position in the list. For symmetric objects the reference position is often the center of the object. In many cases the object's reference position may be explicitly specified by the application.

Specified by:
getReferencePosition in interface Movable
Returns:
the object's reference position, or null if no reference position is available.

getSectors

public List<Sector> getSectors(DrawContext dc)
Returns a List of Sector instances that bound the surface object on the specified DrawContext.

Specified by:
getSectors in interface SurfaceObject
Parameters:
dc - the DrawContext the surface object is related to.
Returns:
the surface object's bounding Sectors.

getText

public CharSequence getText()
Indicates the text contained in this object.

Specified by:
getText in interface GeographicText
Returns:
The current text.

getTextRenderer

protected TextRenderer getTextRenderer(DrawContext dc)
Get the text renderer to use to draw text.

Parameters:
dc - Current draw context.
Returns:
The text renderer that will be used to draw the surface text.

getTextSize

public double getTextSize()

isSmall

protected boolean isSmall()
Determine if the text is too small to draw.

Returns:
true if the height of the text is less than one pixel.

move

public void move(Position position)
Shift the shape over the globe's surface while maintaining its original azimuth, its orientation relative to North.

Specified by:
move in interface Movable
Parameters:
position - the latitude and longitude to add to the shape's reference position.

moveTo

public void moveTo(Position position)
Move the shape over the globe's surface while maintaining its original azimuth, its orientation relative to North.

Specified by:
moveTo in interface Movable
Parameters:
position - the new position of the shape's reference position.

preRender

public void preRender(DrawContext dc)
Causes the surface object to prepare a representation of itself which can be drawn on the surface terrain, using the provided draw context.

Specified by:
preRender in interface PreRenderable
Specified by:
preRender in interface SurfaceObject
Overrides:
preRender in class AbstractSurfaceObject
Parameters:
dc - the current draw context.

setBackgroundColor

public void setBackgroundColor(Color background)
Specifies the background color used to draw the text.

Specified by:
setBackgroundColor in interface GeographicText
Parameters:
background - New background color.

setColor

public void setColor(Color color)
Specifies the color used to draw the text.

Specified by:
setColor in interface GeographicText
Parameters:
color - New color.

setFont

public void setFont(Font font)
Specifies the font used to draw the text.

Specified by:
setFont in interface GeographicText
Parameters:
font - New font.

setOffset

public void setOffset(Offset offset)
Specifies a location relative to the label position at which to align the label. The label text begins at the point indicated by the offset. An offset of (0, 0) aligns the left baseline of the text with the position. An offset of (-0.5, -0.5) fraction aligns the center of the text with the position.

A pixel based offset is interpreted based on the geographic size of the text. For example, if the text rendered "normally" in two dimensions would be 20 pixels tall, and the geographic text is 100 meters tall, then each pixel of text corresponds to 5 meters. So an offset of 2 pixels would correspond to a geographic offset of 10 meters.

Parameters:
offset - Offset that controls where to position the label relative to its geographic location.

setPosition

public void setPosition(Position position)
Specifies the geographic position of the text.

The offset determines how the text is placed relative to this position. The default offset centers the text on the position both horizontally and vertically.

Specified by:
setPosition in interface GeographicText
Parameters:
position - New text position.
See Also:
setOffset(Offset)

setPriority

public void setPriority(double priority)
Specifies the text priority. The priority can be used to implement text culling.

Specified by:
setPriority in interface GeographicText
Parameters:
priority - New priority.

setText

public void setText(CharSequence text)
Specifies the text.

Specified by:
setText in interface GeographicText
Parameters:
text - New text.

setTextSize

public void setTextSize(double meters)

updateTextBounds

protected void updateTextBounds(DrawContext dc)
Determine the text bounds.

Parameters:
dc - Current draw context.

NASA World Wind