arcgissamples\cartography\CustomMarkerSymbol.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create marker symbol
arcgissamples\cartography\CustomMarkerSymbol.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.display.IColor;
import com.esri.arcgis.display.IMarkerSymbol;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.display.esriSimpleMarkerStyle;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPolygon;
import com.esri.arcgis.geometry.ITransformation;
import com.esri.arcgis.system.IClone;
import com.esri.arcgis.interop.AutomationException;

/**
 * Sample application to demonstrate creation of a custom marker symbol.
 * CustomMarkerSymbol will draw boundary of a rectangle, and then paint a
 * circle within the rectangle.
 * The class implements IClone, ISymbol, IMarkerSymbol.
 */
public class CustomMarkerSymbol implements IClone, ISymbol, IMarkerSymbol {

  private static final long serialVersionUID = 1L;
  int HDC;
  ITransformation transformation;
  double size;
  IColor color;
  IColor whiteColor;

  /**
   * Default constructor.
   */
  CustomMarkerSymbol() {
    try {
      this.size = 10;
      RgbColor clr = new RgbColor();
      clr.setRGB(0x0000FF); // red
      this.color = clr;
      RgbColor wColor = new RgbColor();
      wColor.setRed(255);
      wColor.setGreen(255);
      wColor.setBlue(255);
      this.whiteColor = wColor;
    } catch( IOException e) {
      // hever happened
    }
  }

  // IClone interface

  /**
   * @see IClone#esri_clone
   */
  public IClone esri_clone() throws IOException, AutomationException {
    // create a new MyMarkerSymbol
    CustomMarkerSymbol markerSym = new CustomMarkerSymbol();
    // set size and color
    markerSym.setSize(getSize());
    markerSym.setColor(getColor());
    return markerSym;
  }

  /**
   * @see IClone#assign
   */
  public void assign(IClone iClone) {
    // no implement
  }

  /**
   * @see IClone#isEqual
   */
  public boolean isEqual(IClone clone) throws IOException, AutomationException {
    // no need to go further if two objects are of different types
    if( !(clone instanceof CustomMarkerSymbol) )
      return false;
    // QI for IMarkerSymbol from IClone
    IMarkerSymbol markerSym = (IMarkerSymbol)(clone);
    // two MyMarkerSymbols are considered IsEqual if:
    // 1. their Size are equal
    // 2. they have the same color
    if( markerSym.getSize() == getSize() && markerSym.getColor().getRGB() == getColor().getRGB() )
      return true;
    return false;
  }

  /**
   * @see IClone#isIdentical
   */
  public boolean isIdentical(IClone clone) {
    if( !(clone instanceof CustomMarkerSymbol) )
      return false;
    return clone.equals(this);
  }

  // ISymbol interface

  /**
   * @see ISymbol#setupDC
   */
  public void setupDC(int i, ITransformation trans) {
    this.HDC = i;
    this.transformation = trans;
  }

  /**
   * @see ISymbol#resetDC
   */
  public void resetDC() {
    // no implement
  }

  /**
   * @see ISymbol#draw
   */
  public void draw(IGeometry geometry) throws IOException, AutomationException {
    if( geometry == null )
      return;
    // Because we haven't an access to the low level API (like WinAPI draw* functions)
    // we will combine the custom symbol from generic ArcGIS symbols.
    // draw box
    SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol();
    simpleMarkerSymbol.setSize(this.size);
    simpleMarkerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSSquare);
    simpleMarkerSymbol.setColor(this.color);
    simpleMarkerSymbol.setupDC(this.HDC, this.transformation);
    simpleMarkerSymbol.draw(geometry);
    simpleMarkerSymbol.resetDC();
    // draw circle
    SimpleMarkerSymbol simpleMarkerSymbol2 = new SimpleMarkerSymbol();
    simpleMarkerSymbol2.setSize(this.size/2);
    simpleMarkerSymbol2.setStyle(esriSimpleMarkerStyle.esriSMSCircle);
    simpleMarkerSymbol2.setColor(this.whiteColor);
    simpleMarkerSymbol2.setupDC(this.HDC, this.transformation);
    simpleMarkerSymbol2.draw(geometry);
    simpleMarkerSymbol2.resetDC();
  }

  /**
   * @see ISymbol#queryBoundary
   */
  public void queryBoundary(int i, ITransformation tran, IGeometry geometry, IPolygon polygon) {
    // no implement
  }

  /**
   * @see ISymbol#getROP2
   */
  public int getROP2() {
    // no implement
    return 1;
  }

  /**
   * @see ISymbol#setROP2
   * @param i
   */
  public void setROP2(int i) {
    // no implement
  }

  // IMarkerSymbol interface

  /**
   * @see IMarkerSymbol#getSize
   */
  public double getSize() {
    return this.size;
  }

  /**
   * @see IMarkerSymbol#setSize
   */
  public void setSize(double v) {
    this.size = v;
  }

  /**
   * @see IMarkerSymbol#getColor
   */
  public IColor getColor() {
    return this.color;
  }

  /**
   * @see IMarkerSymbol#setColor
   */
  public void setColor(IColor clr) {
    this.color = clr;
  }

  /**
   * @see IMarkerSymbol#getAngle
   */
  public double getAngle() {
    // no implement
    return 0;
  }

  /**
   * @see IMarkerSymbol#setAngle
   */
  public void setAngle(double v) {
    // no implement
  }

  /**
   * @see IMarkerSymbol#getXOffset
   */
  public double getXOffset() {
    // no implement
    return 0;
  }

  /**
   * @see IMarkerSymbol#setXOffset
   */
  public void setXOffset(double v) {
    // no implement
  }

  /**
   * @see IMarkerSymbol#getYOffset
   */
  public double getYOffset() {
    // no implement
    return 0;
  }

  /**
   * @see IMarkerSymbol#setYOffset
   */
  public void setYOffset(double v) {
    // no implement
  }

}