arcgissamples\addins\tool\DrawPolygonGraphicsTool.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.addins.tool; import java.awt.event.MouseEvent; import com.esri.arcgis.addins.desktop.Tool; import com.esri.arcgis.arcmapui.IMxDocument; import com.esri.arcgis.carto.IElement; import com.esri.arcgis.carto.PolygonElement; import com.esri.arcgis.display.IRubberBand; import com.esri.arcgis.display.IScreenDisplay; import com.esri.arcgis.display.ISimpleFillSymbol; import com.esri.arcgis.display.ISymbol; import com.esri.arcgis.display.RubberPolygon; import com.esri.arcgis.display.SimpleFillSymbol; import com.esri.arcgis.framework.IApplication; import com.esri.arcgis.geometry.IPolygon; public class DrawPolygonGraphicsTool extends Tool { IScreenDisplay screenDisplay; IRubberBand rubberPolygon; IPolygon polygon; IApplication app; IMxDocument mxDoc; // Called when the tool is activated by clicking it @Override public void activate() { } // Returns whether this tool is checked public boolean isChecked() { return false; } // Returns whether this tool is enabled public boolean isEnabled() { return true; } // Called when the mouse is moved while the tool is active @Override public void mouseMoved(MouseEvent me) { } // Called when a mouse tool is pressed while the tool is active @Override public void mousePressed(MouseEvent me) { try { mxDoc = (IMxDocument) app.getDocument(); IPolygon polygon=getPolygon(); IElement element=new PolygonElement(); element.setGeometry(polygon); element.activate(mxDoc.getActiveView().getScreenDisplay()); mxDoc.getActiveView().getGraphicsContainer().addElement(element,0); mxDoc.getActiveView().refresh(); } catch(Exception ex) { ex.printStackTrace(); } } // Initializes this tool with the ArcGIS application it is hosted in @Override public void init(IApplication app) { this.app = app; } // Create a polygon filled with a blue color. public IPolygon getPolygon() throws Exception{ ISimpleFillSymbol fillSymbol=new SimpleFillSymbol(); screenDisplay = mxDoc.getActiveView().getScreenDisplay(); rubberPolygon = new RubberPolygon(); polygon = (IPolygon) rubberPolygon.trackNew(screenDisplay, (ISymbol) fillSymbol); return polygon; } }