Optimized_Renderer
OptimizedRendererMain.java
/*
 * ArcGIS Engine Developer Sample
 * Application Name: OptimizedRendererMain.java
 */

package com.esri.arcgis.samples.carto.renderers.optimizedrenderer;

import java.awt.*;
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 javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapControl;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 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 {

  MapControl mapControl = new MapControl();
  Panel toolPanel = new Panel();
  Label labelComment = new Label();
  Button buttonOpenFile = new Button();
  Button buttonRefresh = new Button();
  JRadioButton radioLow = new JRadioButton();
  JRadioButton radioHigh = new JRadioButton();

  private OptimizedRenderer renderer = new OptimizedRenderer();

  /**
   * Constructor
   */
  public OptimizedRendererMain() {
    try {
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Method to start the program execution.
   */
  public static void main(String s[]) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      EngineInitializer.initializeVisualBeans();
      AoInitialize aoint = new AoInitialize();
    int stat = aoint
        .initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
    System.out.println("License is verified");
      OptimizedRendererMain application = new OptimizedRendererMain();
      application.show();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * This is a private method created by UI JBuilder design.
   * @throws java.lang.Exception
   */
  private void jbInit() throws Exception {
    this.setTitle("OptimizedRenderer Sample Application");
    this.setSize(new Dimension(512, 512));
    this.getContentPane().setLayout(new BorderLayout());

    // turn on if you like to design in JBuilder
    //this.getContentPane().setLayout(null);
    //mapControl.setBounds(new Rectangle(11, 8, 483, 365));

    labelComment.setText("Open developerkit/samples/data/usa/countries.shp file.");
    labelComment.setBounds(new Rectangle(10, 10, 370, 26));
    buttonOpenFile.setLabel("Open File ...");
    buttonOpenFile.setBounds(new Rectangle(387, 10, 91, 25));
    buttonRefresh.setBounds(new Rectangle(387, 57, 92, 25));
    buttonRefresh.setLabel("Refresh");
    toolPanel.setBackground(new Color(212, 210, 177));
    toolPanel.setBounds(new Rectangle(7, 382, 489, 109));
    radioLow.setText("Low Speed Method");
    radioLow.setBounds(new Rectangle(10, 62, 200, 20));
    radioLow.setSelected(true);
    radioHigh.setText("High Speed Method");
    radioHigh.setBounds(new Rectangle(209, 61, 200, 20));
    radioHigh.setSelected(false);

    toolPanel.setLayout(null);
    toolPanel.add(buttonOpenFile, null);
    toolPanel.add(labelComment, null);
    toolPanel.add(radioLow, null);
    toolPanel.add(radioHigh, null);
    toolPanel.add(buttonRefresh, null);

    this.getContentPane().add(mapControl, BorderLayout.CENTER);
    this.getContentPane().add(toolPanel, BorderLayout.SOUTH);
    this.setVisible(true);

    // set listeners
    buttonOpenFile.addActionListener(this);
    buttonRefresh.addActionListener(this);
    radioLow.addActionListener(this);
    radioHigh.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object eventSource = event.getSource();
      if (eventSource == buttonOpenFile) {
        try {
          FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
          fileDialog.show();
          String fullFilename = fileDialog.getDirectory() + fileDialog.getFile();
          String path = fullFilename.substring(0, fullFilename.lastIndexOf(File.separator));
          String name = fullFilename.substring(fullFilename.lastIndexOf(File.separator) + 1);
          mapControl.clearLayers();
          mapControl.addShapeFile(path, name);
          FeatureLayer featureLayer = new FeatureLayer(mapControl.getLayer(0));
          featureLayer.setRendererByRef(renderer);
          mapControl.refresh(esriViewDrawPhase.esriViewGeography, null, null);
          System.out.println("Open file");
        } catch (java.lang.Exception ex) {
          System.out.println("Can't open file.");
        }
      }
      if (eventSource == buttonRefresh) {
        try {
          mapControl.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        } catch (java.lang.Exception ex) {
        }
      }
      if (eventSource == radioLow) {
        renderer.setFastDraw(false);
        radioLow.setSelected(true);
        radioHigh.setSelected(false);
      }
      if (eventSource == radioHigh) {
        renderer.setFastDraw(true);
        radioLow.setSelected(false);
        radioHigh.setSelected(true);
      }
  }

}