arcgissamples\cartography\ScaleRenderer.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Scale renderer
arcgissamples\cartography\ScaleRenderer.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.cartography;

import java.io.IOException;

import com.esri.arcgis.carto.IFeatureIDSet;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.display.IDisplay;
import com.esri.arcgis.display.IDisplayTransformation;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.display.esriSimpleFillStyle;
import com.esri.arcgis.display.esriSimpleMarkerStyle;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IQueryFilter;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.extn.ArcGISExtension;
import com.esri.arcgis.system.ITrackCancel;
import com.esri.arcgis.system.esriDrawPhase;

/**
 * This sample demonstrates how to create and use a custom renderer.
 * This renderer will change how polygon features are drawn based on the current map scale.
 */
@ArcGISExtension
public class ScaleRenderer implements IFeatureRenderer {

  private static final long serialVersionUID = 1L;
  SimpleMarkerSymbol markerSymbol;
  SimpleFillSymbol simpleFillSymbol;

  /**
   * Default constructor creates SimpleMarker and SimpleFill sumbols.
   */
  public ScaleRenderer() {
    try {
      // color
      RgbColor color = new RgbColor();
      color.setRGB(0xFF); // red color
      // establish the point symbol
      this.markerSymbol = new SimpleMarkerSymbol();
      this.markerSymbol.setSize(2.0);
      this.markerSymbol.setColor(color);
      this.markerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSCircle);
      // establish the fill symbol
      this.simpleFillSymbol = new SimpleFillSymbol();
      this.simpleFillSymbol.setColor(color);
      this.simpleFillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid);
    } catch (java.lang.Exception e) {
      e.printStackTrace(); // never happened
    }
  }

  // IFeatureRenderer interface

  /**
   * @see IFeatureRenderer#canRender
   * @param featureClass IFeatureClass
   * @param display IDisplay
   * @return boolean
   */
  public boolean canRender(IFeatureClass featureClass, IDisplay display) {
    return true;
  }

  /**
   * @see IFeatureRenderer#prepareFilter
   * @param featureClass IFeatureClass
   * @param queryFilter IQueryFilter
   */
  public void prepareFilter(IFeatureClass featureClass, IQueryFilter queryFilter) {
    // not implemented
  }

  /**
   * @see IFeatureRenderer#draw
   * @param featureCursor IFeatureCursor
   * @param drawPhase int
   * @param display IDisplay
   * @param trackCancel ITrackCancel
   * @throws IOException
   * @throws AutomationException
   */
  public void draw(IFeatureCursor featureCursor, int drawPhase, IDisplay display, ITrackCancel trackCancel) throws IOException, AutomationException {
    IDisplayTransformation displayTrans = display.getDisplayTransformation();
    double scale = displayTrans.getScaleRatio();
    IFeature feature = featureCursor.nextFeature();
    while (feature != null) {
      if (feature.getShape() != null) {
        // draw the feature as a point
        if (scale > 3.0E8) {
          IGeometry geometry = feature.getShape();
          Polygon polygon = (Polygon) geometry;
          Point point = (Point) polygon.getCentroid();

          display.setSymbol(this.markerSymbol);
          display.drawPoint(point);
          // draw the features envelope
        } else if (scale > 1.0E8) {
          Envelope envelope = (Envelope) feature.getShape().getEnvelope();
          display.setSymbol(this.simpleFillSymbol);
          display.drawRectangle(envelope);
          // draw the features geometry
        } else {
          IGeometry geometry = feature.getShape();
          display.setSymbol(this.simpleFillSymbol);
          display.drawPolygon(geometry);
        }
      }
      feature = featureCursor.nextFeature();
    }
  }

  /**
   * @see IFeatureRenderer#getSymbolByFeature
   * @param feature IFeature
   * @return ISymbol
   */
  public ISymbol getSymbolByFeature(IFeature feature) {
    // not implemented
    return null;
  }

  /**
   * @see IFeatureRenderer#isRenderPhase
   * @param drawPhase int
   * @return boolean
   */
  public boolean isRenderPhase(int drawPhase) {
    if (drawPhase == esriDrawPhase.esriDPGeography)
      return true;
    return false;
  }

  /**
   * @see IFeatureRenderer#setExclusionSetByRef
   * @param featureIDSet IFeatureIDSet
   */
  public void setExclusionSetByRef(IFeatureIDSet featureIDSet) {
    // not implemented
  }

}