public class Sprite extends java.lang.Object implements Element
Sprite objects allow to add data representations in a graphic display of a graph. A sprite is a graphical representation that can double anywhere in the graph drawing surface, or be "attached" to nodes or edges. When attached to an edge, a sprite can be positioned easily at any point along the edge, or perpendicular to it with one or two coordinates. When attached to a node, a sprite "orbits" around the node at any given radius and angle around it.
Sprites can have many shapes. Most of the CSS nodes shapes are available for sprites, but more are possible. Some shapes follow the form of the element (node or edge) they are attached to.
Sprites can be moved and animated easily along edges, around nodes, or anywhere on the graph surface. Their shape can change. Some sprites allows to draw pie charts or statistics, or images.
Sprites are not part of a graph so to speak. Furthermore they make sense only
when a graph is displayed with a viewer that supports sprites. Therefore they
are handled by a SpriteManager
which is always associated to a graph
and is in charge of handling the whole set of sprites, creating them,
enumerating them, and destroying them.
Implementation note: sprites do not exist ! In fact the sprite class only handles a set of attributes that are stored in the graph (the one associated with the sprite manager that created the sprite). These special attributes are handled for you by the sprite class. This technique allows to pass sprites informations through the I/O system of GraphStream. Indeed sprites appearing in a graph can therefore be stored in files and retrieved if the graph file format supports attributes. If this is a dynamic graph format, like DGS, the whole sprite history is remembered: when it moved, when it changed, etc.
Second implementation node : often you will need to extend the sprite class.
This is easily possible, but you must remember that you cannot create sprites
yourself, you must use the SpriteManager
. In order to create a sprite
of a special kind, you can either use a SpriteFactory
with the
SpriteManager or the special SpriteManager.addSprite(String, Class)
method of the SpriteManager. In both cases, the
init(String, SpriteManager, Values)
method of the sprite will be
called. Override this method to initialise your sprite.
SpriteManager
,
SpriteFactory
Modifier and Type | Method and Description |
---|---|
boolean |
attached()
True if attached to an edge or node.
|
void |
attachToEdge(java.lang.String id)
Attach the sprite to an edge with the given identifier.
|
void |
attachToNode(java.lang.String id)
Attach the sprite to a node with the given identifier.
|
java.util.stream.Stream<java.lang.String> |
attributeKeys()
Stream over the attribute keys of the element.
|
void |
clearAttributes()
Remove all registered attributes.
|
void |
detach()
Detach the sprite from the element it is attached to (if any).
|
java.lang.Object[] |
getArray(java.lang.String key)
Get the array of objects bound to key.
|
Element |
getAttachment()
The element the sprite is attached to or null if the sprite is not attached.
|
java.lang.Object |
getAttribute(java.lang.String key)
Get the attribute object bound to the given key.
|
<T> T |
getAttribute(java.lang.String key,
java.lang.Class<T> clazz)
Get the attribute object bound to the given key if it is an instance of the
given class.
|
int |
getAttributeCount()
Quite expensive operation !.
|
java.util.Map<java.lang.String,java.lang.Object> |
getAttributeMap() |
<T> T |
getFirstAttributeOf(java.lang.Class<T> clazz,
java.lang.String... keys)
Like
Element.getAttribute(String, Class) , but returns the first existing
attribute in a list of keys, instead of only one key. |
java.lang.Object |
getFirstAttributeOf(java.lang.String... keys)
Like
Element.getAttribute(String) , but returns the first existing attribute
in a list of keys, instead of only one key. |
java.lang.String |
getId()
Unique identifier of this element.
|
int |
getIndex()
The current index of this element
|
java.lang.CharSequence |
getLabel(java.lang.String key)
Get the label string bound to the given key key.
|
java.util.Map<?,?> |
getMap(java.lang.String key)
Get the map bound to key.
|
double |
getNumber(java.lang.String key)
Get the number bound to key.
|
StyleConstants.Units |
getUnits() |
java.util.List<? extends java.lang.Number> |
getVector(java.lang.String key)
Get the vector of number bound to key.
|
double |
getX()
X position.
|
double |
getY()
Y position.
|
double |
getZ()
Z position.
|
boolean |
hasArray(java.lang.String key)
Does this element store an array value for the given key?
|
boolean |
hasAttribute(java.lang.String key)
Does this element store a value for the given attribute key?
|
boolean |
hasAttribute(java.lang.String key,
java.lang.Class<?> clazz)
Does this element store a value for the given attribute key and this value is
an instance of the given class?
|
boolean |
hasLabel(java.lang.String key)
Does this element store a label value for the given key?
|
boolean |
hasMap(java.lang.String key)
Does this element store a map value for the given key?
|
boolean |
hasNumber(java.lang.String key)
Does this element store a number for the given key?
|
boolean |
hasVector(java.lang.String key)
Does this element store a vector value for the given key?
|
void |
removeAttribute(java.lang.String attribute)
Remove an attribute.
|
void |
setAttribute(java.lang.String attribute,
java.lang.Object... values)
Add or replace the value of an attribute.
|
void |
setAttributes(java.util.Map<java.lang.String,java.lang.Object> attributes)
Add or replace each attribute found in attributes.
|
void |
setPosition(double percent) |
void |
setPosition(double x,
double y,
double z) |
void |
setPosition(StyleConstants.Units units,
double x,
double y,
double z) |
public Element getAttachment()
public boolean attached()
public double getX()
public double getY()
public double getZ()
public StyleConstants.Units getUnits()
public void attachToNode(java.lang.String id)
id
- Identifier of the node to attach to.public void attachToEdge(java.lang.String id)
id
- Identifier of the edge to attach to.public void detach()
public void setPosition(double percent)
public void setPosition(double x, double y, double z)
public void setPosition(StyleConstants.Units units, double x, double y, double z)
public java.lang.String getId()
Element
public java.lang.CharSequence getLabel(java.lang.String key)
Element
public java.lang.Object getAttribute(java.lang.String key)
Element
getAttribute
in interface Element
key
- Name of the attribute to search.public <T> T getAttribute(java.lang.String key, java.lang.Class<T> clazz)
Element
getAttribute
in interface Element
key
- The attribute name to search.clazz
- The expected attribute class.public int getAttributeCount()
getAttributeCount
in interface Element
public java.util.stream.Stream<java.lang.String> attributeKeys()
Element
attributeKeys
in interface Element
public java.util.Map<java.lang.String,java.lang.Object> getAttributeMap()
public java.lang.Object getFirstAttributeOf(java.lang.String... keys)
Element
Element.getAttribute(String)
, but returns the first existing attribute
in a list of keys, instead of only one key. The key list order matters.getFirstAttributeOf
in interface Element
keys
- Several strings naming attributes.public <T> T getFirstAttributeOf(java.lang.Class<T> clazz, java.lang.String... keys)
Element
Element.getAttribute(String, Class)
, but returns the first existing
attribute in a list of keys, instead of only one key. The key list order
matters.getFirstAttributeOf
in interface Element
clazz
- The class the attribute must be instance of.keys
- Several string naming attributes.public java.lang.Object[] getArray(java.lang.String key)
Element
public java.util.Map<?,?> getMap(java.lang.String key)
Element
CompoundAttribute
interface are considered like maps since they can
be transformed to a map. If an attribute with the same name exists but is not
a map, null is returned. We cannot enforce the type of the key. It is
considered a string and you should use "Object.toString()" to get it.public double getNumber(java.lang.String key)
Element
public java.util.List<? extends java.lang.Number> getVector(java.lang.String key)
Element
List
of Number
objects.public boolean hasAttribute(java.lang.String key)
Element
hasAttribute
in interface Element
key
- The name of the attribute to search.public boolean hasArray(java.lang.String key)
Element
public boolean hasAttribute(java.lang.String key, java.lang.Class<?> clazz)
Element
hasAttribute
in interface Element
key
- The name of the attribute to search.clazz
- The expected class of the attribute value.public boolean hasMap(java.lang.String key)
Element
Map
) or objects that implement the
CompoundAttribute
class.public boolean hasLabel(java.lang.String key)
Element
public boolean hasNumber(java.lang.String key)
Element
public boolean hasVector(java.lang.String key)
Element
public void setAttribute(java.lang.String attribute, java.lang.Object... values)
Element
setAttribute
in interface Element
attribute
- The attribute name.values
- The attribute value or set of values.public void setAttributes(java.util.Map<java.lang.String,java.lang.Object> attributes)
Element
setAttributes
in interface Element
attributes
- A set of (key,value) pairs.public void clearAttributes()
Element
clearAttributes
in interface Element
public void removeAttribute(java.lang.String attribute)
Element
removeAttribute
in interface Element
attribute
- Name of the attribute to remove.