arcgissamples\cartography\CustomMarkerSymbolMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create marker symbol
arcgissamples\cartography\CustomMarkerSymbolMain.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.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.ISimpleRenderer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * This sample demonstrates how you can create a custom marker symbol.
 * This symbol draws the points in your layer using hollow circles.
 */
public class CustomMarkerSymbolMain extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  Button buttonLoadData = new Button();
  Button buttonSetCustomSymbol = new Button();

  /**
   * Constructor
   */
  public CustomMarkerSymbolMain(String workspacePath, String featureClassName) {
    try {
      initUI(workspacePath, featureClassName);
    } catch (IOException e) {
      System.out.println("Couldn't initialize user interface.");
      e.printStackTrace();
      System.exit(1);
    }
  }

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    System.out.println("Starting Custom Marker Symbol - An ArcObjects Java SDK Developer Sample");
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    //
    // Change the following lines if you want to use different data
    //
    String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa";
    String featureClassName = "wind";
    File shapefileFile = new File(workspacePath, featureClassName + ".shp");
    if (!shapefileFile.exists()) {
      System.out.println("Shapefile does not exist: " + shapefileFile.getAbsolutePath());
      System.exit(0);
    }
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      new CustomMarkerSymbolMain(workspacePath, featureClassName);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeBasic);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI(String path, String name) throws IOException {
    this.setTitle("Custom Marker Symbol Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    Label labelComment = new Label();
    labelComment.setText("Open another marker shapefile, like java/samples/data/usa/wind.shp file.");
    this.buttonLoadData.setLabel("Load Data");
    this.buttonSetCustomSymbol.setLabel("Set Custom Symbol");

    Panel toolPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    toolPanel.add(labelComment);
    toolPanel.add(this.buttonLoadData);
    toolPanel.add(this.buttonSetCustomSymbol);

    this.mapBean.clearLayers();
    this.mapBean.addShapeFile(path, name);

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(toolPanel, BorderLayout.SOUTH);
    this.setVisible(true);
    // set listener
    this.buttonLoadData.addActionListener(this);
    this.buttonSetCustomSymbol.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  /**
   * implements interface ActionListener
   * @see java.awt.event.ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object eventSource = event.getSource();
    if (eventSource == this.buttonLoadData) {
      try {
        FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
        fileDialog.setVisible(true);
        String directoryName = fileDialog.getDirectory();
        String fileName = fileDialog.getFile();
        if (directoryName == null || fileName == null) {
          return; // This could happen if the user presses "cancel" in the dialog
        }
        String fullFilename = fileDialog.getDirectory() + fileDialog.getFile();
        String path = fullFilename.substring(0, fullFilename.lastIndexOf(File.separator));
        String name = fullFilename.substring(fullFilename.lastIndexOf(File.separator) + 1);
        this.mapBean.clearLayers();
        this.mapBean.addShapeFile(path, name);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.lang.Exception ex) {
        ex.printStackTrace(); // never happened
      }
    }
    if (eventSource == this.buttonSetCustomSymbol) {
      try {
        ILayer layer = this.mapBean.getLayer(0);
        FeatureLayer featureLayer = (FeatureLayer) layer;
        CustomMarkerSymbol customMarkerSymbol = new CustomMarkerSymbol();
        IFeatureRenderer featureRenderer = featureLayer.getRenderer();
        ISimpleRenderer simpleRenderer = (ISimpleRenderer)(featureRenderer);
        simpleRenderer.setSymbolByRef(customMarkerSymbol);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.lang.Exception ex) {
        ex.printStackTrace(); // never happened
      }
    }
  }
}