Create a custom line symbol
arcgissamples\cartography\CustomLineSymbolMain.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.cartography;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
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 class provides a framework for demo application.
 */
public class CustomLineSymbolMain 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();
  Button buttonZoomIn = new Button();
  Button buttonZoomOut = new Button();

  /**
   * Constructor
   */
  public CustomLineSymbolMain(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 Line 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 = "ushigh";
    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 CustomLineSymbolMain(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.esriLicenseProductCodeArcView) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
    } 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 Line Symbol Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    Label labelComment = new Label();
    labelComment.setText("Open java/samples/data/usa/ushigh.shp file.");
    this.buttonLoadData.setLabel("Load Data");
    this.buttonSetCustomSymbol.setLabel("Custom Symbol");
    this.buttonZoomIn.setLabel("ZoomIn");
    this.buttonZoomOut.setLabel("ZoomOut");

    Panel loadPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    loadPanel.add(labelComment);
    loadPanel.add(this.buttonLoadData);
    Panel zoomPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    zoomPanel.add(this.buttonZoomIn);
    zoomPanel.add(this.buttonZoomOut);
    zoomPanel.add(this.buttonSetCustomSymbol);

    Panel toolPanel = new Panel();
    toolPanel.setLayout(new GridLayout(2,1));
    toolPanel.add(loadPanel);
    toolPanel.add(zoomPanel);

    Panel nestPanel = new Panel();
    nestPanel.add(toolPanel);

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

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(nestPanel, BorderLayout.SOUTH);
    this.setVisible(true);
    // set listener
    this.buttonLoadData.addActionListener(this);
    this.buttonSetCustomSymbol.addActionListener(this);
    this.buttonZoomIn.addActionListener(this);
    this.buttonZoomOut.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 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 happens
      }
    }
    if (eventSource == this.buttonSetCustomSymbol) {
      try {
        ILayer layer = this.mapBean.getLayer(0);
        FeatureLayer featureLayer = (FeatureLayer) layer;
        CustomLineSymbol customMarkerSymbol = new CustomLineSymbol();
        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 happens
      }
    }
    if (eventSource == this.buttonZoomIn) {
      try {
        double mapScale = this.mapBean.getMapScale();
        this.mapBean.setMapScale(mapScale / 1.2);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.io.IOException ex) {
        ex.printStackTrace(); // never happens
      }
    }
    if (eventSource == this.buttonZoomOut) {
      try {
        double mapScale = this.mapBean.getMapScale();
        this.mapBean.setMapScale(mapScale * 1.2);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.io.IOException ex) {
        ex.printStackTrace(); // never happens
      }
    }
  }
}