arcgissamples\cartography\OptimizedRendererMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Optimized renderer
arcgissamples\cartography\OptimizedRendererMain.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.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.JRadioButton;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.SimpleRenderer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineContext;
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.
 * Use at least 1mb data size to see the result of perfomance comparison.
 */
public class OptimizedRendererMain extends JFrame implements ActionListener {
  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  ToolbarBean toolbarBean = null;
  Label labelComment = new Label();
  Button buttonOpenFile = new Button();
  Button buttonClear = new Button();
  JRadioButton radioLow = new JRadioButton();
  JRadioButton radioHigh = new JRadioButton();
  IFeatureRenderer originalRenderer;
  IOptimizedRenderer renderer;
    
  /**
   * Constructor
   * @throws Exception 
   */
  public OptimizedRendererMain() throws Exception {
    try {
      originalRenderer=new SimpleRenderer();
    
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    initUI();
  }
  
  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      new OptimizedRendererMain();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
    } catch (Exception e) {e.printStackTrace();}
  }

  /*
   * Lays out the Visual Controls, and sets up event listeners
   */
  private void initUI() throws Exception {
    this.setTitle("OptimizedRenderer Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());
    
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    this.labelComment.setText("Open " + devKitHome + File.separator + "java" + File.separator + "samples" 
                             + File.separator + "data" + File.separator + "usa"
                             + File.separator + "counties.shp file.");
        
    this.buttonOpenFile.setLabel("Open File ...");
    this.buttonClear.setLabel("Clear");
    this.radioLow.setText("Low Speed Method");
    this.radioLow.setSelected(false);
    this.radioHigh.setText("High Speed Method");
    
    Panel loadPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    loadPanel.add(this.labelComment);
    loadPanel.add(this.buttonOpenFile);
    Panel radioPanel = new Panel(new FlowLayout(FlowLayout.LEADING));
    radioPanel.add(this.radioLow);
    radioPanel.add(this.radioHigh);
    radioPanel.add(this.buttonClear);
    
    Panel toolPanel = new Panel(new GridLayout(2,1));
    toolPanel.add(loadPanel);
    toolPanel.add(radioPanel);
    
    Panel nestPanel = new Panel();
    nestPanel.add(toolPanel);
    
    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(getToolbarBean(), BorderLayout.NORTH);
    this.getContentPane().add(nestPanel, BorderLayout.SOUTH);
    this.setVisible(true);
    
    // set listeners
    this.buttonOpenFile.addActionListener(this);
    this.buttonClear.addActionListener(this);
    this.radioLow.addActionListener(this);
    this.radioHigh.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }
  
  private ToolbarBean getToolbarBean() throws Exception {
    if (toolbarBean == null) {
      toolbarBean = new ToolbarBean();
      toolbarBean.setItemsString("4|controls/ControlsAddDataCommand|0|-1|0|0|1;4|controls/ControlsOpenDocCommand|0|-1|0|0|1;4|controls/ControlsSaveAsDocCommand|0|-1|0|0|1;10|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomOutFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInTool|0|-1|0|0|1;10|controls/ControlsMapZoomOutTool|0|-1|0|0|1;10|controls/ControlsMapPanTool|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentBackCommand|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentForwardCommand|0|-1|0|0|1;3|controls/ControlsClearSelectionCommand|0|-1|0|0|1;3|controls/ControlsSelectAllCommand|0|-1|0|0|1;3|controls/ControlsSelectByGraphicsCommand|0|-1|0|0|1;3|controls/ControlsZoomToSelectedCommand|0|-1|0|0|1;3|controls/ControlsSelectFeaturesTool|0|-1|0|0|1;4|controls/ControlsMapIdentifyTool|0|-1|0|0|1");
      toolbarBean.setBuddyControl(mapBean);
    }
    return toolbarBean;
  }

  
  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    
    Object eventSource = event.getSource();
    
    /* On "Open File" Click Event Load the Shp File*/
    if (eventSource == this.buttonOpenFile) {
      try {
        renderer = (IOptimizedRenderer) EngineContext.createObject(OptimizedRenderer.class);
        //Open a shp File
        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();
        mapBean.addShapeFile(path, name);
        
        //save its original renderer
        FeatureLayer featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
        originalRenderer=featureLayer.getRenderer();
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        
      } catch (java.lang.Exception ex) {
        System.out.println("Can't open file, exception:" + ex);
      }
    }
    
    //On "Clear" Button Click Event restore the original renderer
    if (eventSource == this.buttonClear) {
      try {
        if (mapBean!=null){
          FeatureLayer featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
          featureLayer.setRendererByRef(originalRenderer);
          this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);  
        }
      } catch (java.lang.Exception ex) {
        //never happened
      }
      this.radioLow.setSelected(false);
      this.radioHigh.setSelected(false);
      
    }
    /* On "Low Speed Method" click event
     * Set the boolean fast variable to false 
     * And render the feature layer using this.renderer 
     */
    if (eventSource == this.radioLow) {
      try {
        if (this.mapBean!=null){
          FeatureLayer featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
          renderer.setFastDraw(false);
          featureLayer.setRendererByRef((IFeatureRenderer) renderer);
          this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        }
      } catch (IOException e) {
      
        e.printStackTrace();
      }
      this.radioLow.setSelected(true);
      this.radioHigh.setSelected(false);
    }
    
    
    /* On "High Speed Method" Click Event 
     * Set the boolean fast variable to true 
     * And render the feature layer using this.renderer 
    */
    if (eventSource==this.radioHigh){
      try {
        if(this.mapBean!=null){
          FeatureLayer featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
          renderer.setFastDraw(true);
          featureLayer.setRendererByRef((IFeatureRenderer) renderer);
          this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }  
      this.radioLow.setSelected(false);
      this.radioHigh.setSelected(true);
    }
    
  }
  
}