Class Sprite
- All Implemented Interfaces:
Element
public class Sprite extends 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.
- See Also:
SpriteManager,SpriteFactory
-
Method Summary
Modifier and Type Method Description booleanattached()True if attached to an edge or node.voidattachToEdge(String id)Attach the sprite to an edge with the given identifier.voidattachToNode(String id)Attach the sprite to a node with the given identifier.Stream<String>attributeKeys()Stream over the attribute keys of the element.voidclearAttributes()Remove all registered attributes.voiddetach()Detach the sprite from the element it is attached to (if any).Object[]getArray(String key)Get the array of objects bound to key.ElementgetAttachment()The element the sprite is attached to or null if the sprite is not attached.ObjectgetAttribute(String key)Get the attribute object bound to the given key.<T> TgetAttribute(String key, Class<T> clazz)Get the attribute object bound to the given key if it is an instance of the given class.intgetAttributeCount()Quite expensive operation !.Map<String,Object>getAttributeMap()<T> TgetFirstAttributeOf(Class<T> clazz, String... keys)LikeElement.getAttribute(String, Class), but returns the first existing attribute in a list of keys, instead of only one key.ObjectgetFirstAttributeOf(String... keys)LikeElement.getAttribute(String), but returns the first existing attribute in a list of keys, instead of only one key.StringgetId()Unique identifier of this element.intgetIndex()The current index of this elementCharSequencegetLabel(String key)Get the label string bound to the given key key.Map<?,?>getMap(String key)Get the map bound to key.doublegetNumber(String key)Get the number bound to key.StyleConstants.UnitsgetUnits()List<? extends Number>getVector(String key)Get the vector of number bound to key.doublegetX()X position.doublegetY()Y position.doublegetZ()Z position.booleanhasArray(String key)Does this element store an array value for the given key?booleanhasAttribute(String key)Does this element store a value for the given attribute key?booleanhasAttribute(String key, Class<?> clazz)Does this element store a value for the given attribute key and this value is an instance of the given class?booleanhasLabel(String key)Does this element store a label value for the given key?booleanhasMap(String key)Does this element store a map value for the given key?booleanhasNumber(String key)Does this element store a number for the given key?booleanhasVector(String key)Does this element store a vector value for the given key?voidremoveAttribute(String attribute)Remove an attribute.voidsetAttribute(String attribute, Object... values)Add or replace the value of an attribute.voidsetAttributes(Map<String,Object> attributes)Add or replace each attribute found in attributes.voidsetPosition(double percent)voidsetPosition(double x, double y, double z)voidsetPosition(StyleConstants.Units units, double x, double y, double z)
-
Method Details
-
getAttachment
The element the sprite is attached to or null if the sprite is not attached.- Returns:
- An element the sprite is attached to or null.
-
attached
public boolean attached()True if attached to an edge or node.- Returns:
- False if not attached.
-
getX
public double getX()X position.- Returns:
- The position in abscissa.
-
getY
public double getY()Y position.- Returns:
- The position in ordinate.
-
getZ
public double getZ()Z position.- Returns:
- The position in depth.
-
getUnits
-
attachToNode
Attach the sprite to a node with the given identifier. If needed the sprite is first detached. If the given node identifier does not exist, the sprite stays in detached state.- Parameters:
id- Identifier of the node to attach to.
-
attachToEdge
Attach the sprite to an edge with the given identifier. If needed the sprite is first detached. If the given edge identifier does not exist, the sprite stays in detached state.- Parameters:
id- Identifier of the edge to attach to.
-
detach
public void detach()Detach the sprite from the element it is attached to (if any). -
setPosition
public void setPosition(double percent) -
setPosition
public void setPosition(double x, double y, double z) -
setPosition
-
getId
Description copied from interface:ElementUnique identifier of this element. -
getLabel
Description copied from interface:ElementGet the label string bound to the given key key. Labels are special attributes whose value is a character sequence. If an attribute with the same name exists but is not a character sequence, null is returned. -
getAttribute
Description copied from interface:ElementGet the attribute object bound to the given key. The returned value may be null to indicate the attribute does not exists or is not supported.- Specified by:
getAttributein interfaceElement- Parameters:
key- Name of the attribute to search.- Returns:
- The object bound to the given key or null if no object match this attribute name.
-
getAttribute
Description copied from interface:ElementGet the attribute object bound to the given key if it is an instance of the given class. Some The returned value maybe null to indicate the attribute does not exists or is not an instance of the given class.- Specified by:
getAttributein interfaceElement- Parameters:
key- The attribute name to search.clazz- The expected attribute class.- Returns:
- The object bound to the given key or null if no object match this attribute.
-
getAttributeCount
public int getAttributeCount()Quite expensive operation !.- Specified by:
getAttributeCountin interfaceElement- Returns:
- the number of attributes.
-
attributeKeys
Description copied from interface:ElementStream over the attribute keys of the element. If no attribute exist, method will return empty stream.- Specified by:
attributeKeysin interfaceElement- Returns:
- a String stream corresponding to the keys of the attributes.
-
getAttributeMap
-
getFirstAttributeOf
Description copied from interface:ElementLikeElement.getAttribute(String), but returns the first existing attribute in a list of keys, instead of only one key. The key list order matters.- Specified by:
getFirstAttributeOfin interfaceElement- Parameters:
keys- Several strings naming attributes.- Returns:
- The first attribute that exists.
-
getFirstAttributeOf
Description copied from interface:ElementLikeElement.getAttribute(String, Class), but returns the first existing attribute in a list of keys, instead of only one key. The key list order matters.- Specified by:
getFirstAttributeOfin interfaceElement- Parameters:
clazz- The class the attribute must be instance of.keys- Several string naming attributes.- Returns:
- The first attribute that exists.
-
getArray
Description copied from interface:ElementGet the array of objects bound to key. Arrays of objects are special attributes whose value is a sequence of objects. If an attribute with the same name exists but is not an array, null is returned. -
getMap
Description copied from interface:ElementGet the map bound to key. Maps are special attributes whose value is a set of pairs (name,object). Instances of object implementing theCompoundAttributeinterface 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. -
getNumber
Description copied from interface:ElementGet the number bound to key. Numbers are special attributes whose value is an instance of Number. If an attribute with the same name exists but is not a Number, NaN is returned. -
getVector
Description copied from interface:Element -
hasAttribute
Description copied from interface:ElementDoes this element store a value for the given attribute key? Note that returning true here does not mean that calling getAttribute with the same key will not return null since attribute values can be null. This method just checks if the key is present, with no test on the value.- Specified by:
hasAttributein interfaceElement- Parameters:
key- The name of the attribute to search.- Returns:
- True if a value is present for this attribute.
-
hasArray
Description copied from interface:ElementDoes this element store an array value for the given key? Only object arrays (instance of Object[]) are considered as array here. -
hasAttribute
Description copied from interface:ElementDoes this element store a value for the given attribute key and this value is an instance of the given class?- Specified by:
hasAttributein interfaceElement- Parameters:
key- The name of the attribute to search.clazz- The expected class of the attribute value.- Returns:
- True if a value is present for this attribute.
-
hasMap
Description copied from interface:ElementDoes this element store a map value for the given key? A map is a set of pairs (key,value) (Map) or objects that implement theCompoundAttributeclass. -
hasLabel
Description copied from interface:ElementDoes this element store a label value for the given key? A label is an attribute whose value is a char sequence. -
hasNumber
Description copied from interface:ElementDoes this element store a number for the given key? A number is an attribute whose value is an instance of Number. -
hasVector
Description copied from interface:ElementDoes this element store a vector value for the given key? A vector is an attribute whose value is a sequence of numbers. -
setAttribute
Description copied from interface:ElementAdd or replace the value of an attribute. Existing attributes are overwritten silently. All classes inheriting from Number can be considered as numbers. All classes inheriting from CharSequence can be considered as labels. You can pass zero, one or more arguments for the attribute values. If no value is given, a boolean with value "true" is added. If there is more than one value, an array is stored. If there is only one value, the value is stored (but not in an array).- Specified by:
setAttributein interfaceElement- Parameters:
attribute- The attribute name.values- The attribute value or set of values.
-
setAttributes
Description copied from interface:ElementAdd or replace each attribute found in attributes. Existing attributes are overwritten silently. All classes inheriting from Number can be considered as numbers. All classes inheriting from CharSequence can be considered as labels.- Specified by:
setAttributesin interfaceElement- Parameters:
attributes- A set of (key,value) pairs.
-
clearAttributes
public void clearAttributes()Description copied from interface:ElementRemove all registered attributes. This includes numbers, labels and vectors.- Specified by:
clearAttributesin interfaceElement
-
removeAttribute
Description copied from interface:ElementRemove an attribute. Non-existent attributes errors are ignored silently.- Specified by:
removeAttributein interfaceElement- Parameters:
attribute- Name of the attribute to remove.
-
getIndex
public int getIndex()Description copied from interface:ElementThe current index of this element
-