Adding polygon graphics

The sections below will explain how to:

  1. create and add simple polygons to a graphics layer;
  2. create and add multipart and donut polygons to a graphics layer.

Adding simple polygons to a graphics layer

Polygons are added to a graphics layer in three stages:

  1. Creating a symbol to define how the polygon is represented. This is achieved through the use of the SimpleFillSymbol class which allows a user to define:

    • The colour of the polygon fill
    • If the fill has a style (such as cross hatching)
    • The style of the polygon outline if desired.

  2. Creating a polygon geometry using the Polygon class. The class has methods which allow the polygon outline to be built with a series of points. The first point for a polygon is defined using the startPath method and subsequent points are defined using the lineTo method.
  3. Creating a graphic which will be added to the graphics layer. The Graphic class uses the defined symbol and geometry for the polygon.

The code sample below shows how a simple polygon representing a rectangle can be created:

//create a line symbol for the polygon outline (this is optional if an outline is not needed)
SimpleLineSymbol polygonOutline = new SimpleLineSymbol(Color.DARK_GRAY, 2, SimpleLineSymbol.Style.SOLID);
 
//create the polygon symbol (if an outline is not needed put "null" instead of "polygonOutline"
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(Color.green, polygonOutline, SimpleFillSymbol.Style.SOLID);
 
//create the geometry for a polygon
Polygon polygonGeometry = new Polygon();
 
//create the graphic
Graphic polygonGraphic = new Graphic(polygonGeometry, fillSymbol);
 
//add the graphic to the graphics layer
myGraphicsLayer.addGraphic(polygonGraphic);

The code above will display a rectangle which looks like the image below:

screenshot of a simple polygon graphic

Adding multipart and donut polygons to a graphics layer

The section for adding simple polygons showed how to create a polygon which was just a rectangle. The polygon class also has the ability to create multipart polygons of polygons which have holes in them (like a donut).

Multipart polygons may be used to represent the area of a country which consists of a number of islands. Donut polygons may be used to represent an island which has a lake in the middle. It is possible to take this example further as this lake could contain another island which also has its own lake and so on. Below is an example of a complex polygon which consists of the triangle on the left, and the rectangle with a hole in it, on the right:

screenshot of a complex polygon

The geometry shown above was constructed using the following code:

Polygon complexPolygon = new Polygon();
 
//create the main rectangle outline
complexPolygon.startPath(-290012,7596957);
complexPolygon.lineTo(-287760,7596957);
complexPolygon.lineTo(-287760,7598669);
complexPolygon.lineTo(-290012,7598669);
 
//create a hole in the rectangle
complexPolygon.startPath(-289568,7598217);
complexPolygon.lineTo(-288265,7598217);
complexPolygon.lineTo(-288265,7597389);
complexPolygon.lineTo(-289568,7597389);
 
//add the triangle to the left of the rectangle
complexPolygon.startPath(-290444,7598224);
complexPolygon.lineTo(-290394,7597281);
complexPolygon.lineTo(-291287,7597942);

This geometry can now be used to create a graphic which can be added to the graphics layer as in the section showing how a simple polygon graphic is added.

2/7/2013