Controlling the drawing order of graphics
If you want a set of graphics to appear on top of another set of graphics, the simplest way to do so is to have two graphics layers and to add them to your map in the correct sequence: the last layer added will appear on top of the other layers. However, in some situations you may want to control which graphics appear on top of others within a graphics layer. For example, you may want graphics selected via a mouse action or a query to appear above non-selected graphics, without having to move them to another graphics layer. The API provides you with methods to accomplish this, primarily through the GraphicsLayer class. Note that because the feature layer class, ArcGISFeatureLayer, inherits from GraphicsLayer, these methods are applicable to feature layers as well. A geometry's z value (height) does not have an effect on it's draw order.
Draw order property
Each graphic in a graphics layer has a draw order property associated with it. As graphics cannot be modified once they are created, this property can be set at creation time, then only obtained from the graphic. The default value of this property is 0 if it is left unspecified at creation time. A graphic with a higher draw order value than another will be drawn above that graphic in the map. If two or more graphics have the same draw order value, the order in which they will appear will be determined by the order in which they were added to the graphics layer: the last graphic added will appear on top, and so on.
Modifying the drawing order
Through the GraphicsLayer class, you can have fine-grained control over the drawing order of the graphics in a graphics layer. When you add a graphic to a graphics layer, the graphic appears in the layer and the method call returns a unique graphic id. By storing its graphic id, you can modify the drawing order of this graphic once it is in the layer. You can:
- bring a graphic to the front, to be drawn above all the other graphics in its layer
- move a graphic to the back, to be drawn below all the other graphics in its layer
- update the drawing order of a graphic to a particular value
- update the drawing order of many graphics to one or more particular values.
These methods are used in an interactive application which is available in the Java Sample Application under the table of content heading Mapping > Graphics.