How to draw a polygon on the screen


Summary
This article shows how to draw a polygon using IRubberBand. In this example, the polygon will not persist on the screen, but will be removed after the next screen refresh.

Drawing a polygon on the screen

The following code draws a polygon on the screen in the map where the mouse is clicked. The x,y coordinates come from a mousedown click event when the user interacts with the application.
[Java]
static void drawPolygon(IActiveView activeView)throws Exception{
    IScreenDisplay screenDisplay = activeView.getScreenDisplay();

    screenDisplay.startDrawing(screenDisplay.getHDC(), (short)
        esriScreenCache.esriNoScreenCache);
    IRgbColor rgbColor = new RgbColor();
    rgbColor.setRed(255);

    IColor color = rgbColor;
    ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
    simpleFillSymbol.setColor(color);

    ISymbol symbol = (ISymbol)simpleFillSymbol;
    IRubberBand rubberBand = new RubberPolygon();
    IGeometry geometry = rubberBand.trackNew(screenDisplay, symbol);
    screenDisplay.setSymbol(symbol);
    screenDisplay.drawPolygon((IEnvelope)geometry);
    screenDisplay.finishDrawing();
}


See Also:

How to draw a point on the screen
How to draw a polyline on the screen
How to draw a rectangle on the screen




Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine