arcgissamples\cartography\FirstCustomRenderer.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
First custom renderer
arcgissamples\cartography\FirstCustomRenderer.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.ISimpleFillSymbol;
import com.esri.arcgis.display.ISimpleLineSymbol;
import com.esri.arcgis.display.ISimpleMarkerSymbol;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.display.esriSimpleFillStyle;
import com.esri.arcgis.display.esriSimpleLineStyle;
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.IFeatureDraw;
import com.esri.arcgis.geodatabase.IQueryFilter;
import com.esri.arcgis.geodatabase.esriDrawStyle;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.ITrackCancel;
import com.esri.arcgis.system.esriDrawPhase;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.extn.ArcGISExtension;

/**
 * This sample demonstrates how to create a custom renderer.
 * The renderer draws symbology for point, line and polygon feature layers.
 * 
 */
@ArcGISExtension
public class FirstCustomRenderer implements IFeatureRenderer {

  private static final long serialVersionUID = 1L;
  ISymbol symbol;
  ISimpleMarkerSymbol simpleMarkerSymbol;
  ISimpleLineSymbol simpleLineSymbol;
  ISimpleFillSymbol simpleFillSymbol;

  /**
   * Default constructor sets three symbols and their properties.
   */
  FirstCustomRenderer() {
    try {
      // set up a color for symbols (marker, line and fill)
      RgbColor color = new RgbColor();
      color.setRGB(0x0000FF); // red color

      // set up the marker symbol
      this.simpleMarkerSymbol = new SimpleMarkerSymbol();
      this.simpleMarkerSymbol.setSize(12);
      this.simpleMarkerSymbol.setColor(color);
      this.simpleMarkerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSX);

      // set up the line symbol
      this.simpleLineSymbol = new SimpleLineSymbol();
      this.simpleLineSymbol.setWidth(1.0);
      this.simpleLineSymbol.setColor(color);
      this.simpleLineSymbol.setStyle(esriSimpleLineStyle.esriSLSDashDotDot);

      // setup the fill symbol
      this.simpleFillSymbol = new SimpleFillSymbol();
      this.simpleFillSymbol.setColor(color);
      this.simpleFillSymbol.setStyle(esriSimpleFillStyle.esriSFSDiagonalCross);
    } catch (java.lang.Exception e) {
      System.err.println("FirstCustomRenderer constructor, exception " + e);
    }
  }

  // IFeatureRenderer interface

  /**
   * @see IFeatureRenderer#canRender
   * @param featureClass IFeatureClass
   * @param display IDisplay
   * @throws IOException
   * @throws AutomationException
   * @return boolean
   */
  public boolean canRender(IFeatureClass featureClass, IDisplay display) throws IOException, AutomationException {
    if (featureClass.getShapeType() != esriGeometryType.esriGeometryNull)
      return true;
    return false;
  }

  /**
   * This method selects the appropriated symbol depend on feature type.
   * @see IFeatureRenderer#prepareFilter
   * @param featureClass IFeatureClass
   * @param queryFilter IQueryFilter
   * @throws IOException
   * @throws AutomationException
   */
  public void prepareFilter(IFeatureClass featureClass, IQueryFilter queryFilter) throws IOException, AutomationException {
    switch (featureClass.getShapeType()) {
    case esriGeometryType.esriGeometryPoint:
      this.symbol = (ISymbol) this.simpleMarkerSymbol;
      break;
    case esriGeometryType.esriGeometryPolyline:
      this.symbol = (ISymbol) this.simpleLineSymbol;
      break;
    case esriGeometryType.esriGeometryPolygon:
      this.symbol = (ISymbol) this.simpleFillSymbol;
      break;
    }
  }

  /**
   * The method draws feature on the screen by useing appropriated symbol.
   * @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 {
    // do not draw features if no display or wrong drawphase
    if (display == null || drawPhase != esriDrawPhase.esriDPGeography)
      return;
    // set symbol
    display.setSymbol(this.symbol);
    // loop through the features and draw them
    IFeature feature = featureCursor.nextFeature();
    while (feature != null) {
      IFeatureDraw featureDraw = (IFeatureDraw) feature;
      featureDraw.draw(drawPhase, display, this.symbol, false, null, esriDrawStyle.esriDSNormal);
      feature = featureCursor.nextFeature();
    }
  }

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

  /**
   * @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
  }
}