Geodesy Map control
arcgissamples\defensesolutions\MAGeodesy.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.defensesolutions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IFillShapeElement;
import com.esri.arcgis.carto.IGraphicsContainer;
import com.esri.arcgis.carto.ILineElement;
import com.esri.arcgis.carto.LineElement;
import com.esri.arcgis.carto.PolygonElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.ControlsMapFullExtentCommand;
import com.esri.arcgis.controls.ControlsMapPanTool;
import com.esri.arcgis.controls.ControlsMapZoomInTool;
import com.esri.arcgis.controls.ControlsMapZoomOutTool;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
import com.esri.arcgis.controls.IMapControlEvents2;
import com.esri.arcgis.controls.IMapControlEvents2OnAfterDrawEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnAfterScreenDrawEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnBeforeScreenDrawEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnDoubleClickEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnExtentUpdatedEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnFullExtentUpdatedEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnKeyDownEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnKeyUpEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMapReplacedEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseUpEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnOleDropEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnSelectionChangedEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnViewRefreshedEvent;
import com.esri.arcgis.controls.ITOCControlEventsOnMouseUpEvent;
import com.esri.arcgis.controls.MapControl;
import com.esri.arcgis.controls.ToolbarControl;
import com.esri.arcgis.defensesolutions.GeoEllipse;
import com.esri.arcgis.defensesolutions.GeoEllipseElement;
import com.esri.arcgis.defensesolutions.GeoPolygon;
import com.esri.arcgis.defensesolutions.GeoPolygonElement;
import com.esri.arcgis.defensesolutions.GeoPolyline;
import com.esri.arcgis.defensesolutions.IGeoEllipse;
import com.esri.arcgis.defensesolutions.IGeoPolygon;
import com.esri.arcgis.defensesolutions.MeasurementTool;
import com.esri.arcgis.defensesolutions.cjmtkSGType;
import com.esri.arcgis.display.ArrowMarkerSymbol;
import com.esri.arcgis.display.CartographicLineSymbol;
import com.esri.arcgis.display.HashLineSymbol;
import com.esri.arcgis.display.ICartographicLineSymbol;
import com.esri.arcgis.display.IColor;
import com.esri.arcgis.display.IFillSymbol;
import com.esri.arcgis.display.IHashLineSymbol;
import com.esri.arcgis.display.ILineDecoration;
import com.esri.arcgis.display.ILineDecorationElement;
import com.esri.arcgis.display.ILineProperties;
import com.esri.arcgis.display.ILineSymbol;
import com.esri.arcgis.display.IMarkerSymbol;
import com.esri.arcgis.display.IRgbColor;
import com.esri.arcgis.display.ISimpleLineDecorationElement;
import com.esri.arcgis.display.LineDecoration;
import com.esri.arcgis.display.LineFillSymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleLineDecorationElement;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPolygon;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.esriCommandStyles;



/**
 * <p>Title: MAGeodesy with map control</p>
 * @version 1.0
 */

public class MAGeodesy extends JFrame implements ActionListener,IMapControlEvents2{

  private static final long serialVersionUID = 1L;
  private MapControl mapControl;
  private ToolbarControl toolBarControl;
  private JButton refreshButton,computeButton,clearLineButton;
  private JButton ellipseButton,kpButton,clearButton;
  private JTextField X1Text;
  private JTextField Y1Text ;
  private JTextField X2Text ;
  private JTextField Y2Text ;
  private JTextField aziText ;
  private JTextField distText ;
  private JComboBox lineCbo;


  private JTextField centerX1Text;
  private JTextField centerY1Text;
  private JTextField XAxisText ;
  private JTextField YAxisText ;
  private JTextField rotationText;

  private java.util.Vector pointVector;
  private final String R="Rhumb Line";
  private final String G="Geodesic Line";
  private final String C="Great Circle";

  private IPolygon keypointPolygon;
  @SuppressWarnings("unused")
  private boolean flag = true;
  private IPoint startPoint,endPoint;


  public MAGeodesy(String arcgishome){
    super("MAGeodesy");
    buildGui();

    try{

      String shapePath = arcgishome + java.io.File.separator+"java"+java.io.File.separator+"samples"+java.io.File.separator+"data"+java.io.File.separator+"world"+java.io.File.separator+"Continent.lyr";
      mapControl.addLayerFromFile (shapePath,0);
      addWindowListener(new WindowAdapter() {
        //@Override
        public void windowClosing(WindowEvent e) {
          try {
            new AoInitialize().shutdown();
          } catch (Exception ex) {
            // Don't care
          }
          e.getWindow().dispose();
        }
      });

      this.setSize(650,800);
      this.setVisible(true);
      pointVector = new Vector();

    }
    catch(IOException io){
      JOptionPane.showMessageDialog(this,"Map Control not created correctly: Check if path variable 'arcgishome' is set to c:\\program files\\arcgis ","Error Message",JOptionPane.ERROR_MESSAGE);

    }


  }

  public void buildGui()
  {
    JPanel mainPanel = new JPanel(new BorderLayout());

    mapControl = new MapControl();
    try {
      mapControl.addIMapControlEvents2Listener(this);
    }
    catch (Exception ex) {

    }




    toolBarControl = new ToolbarControl();

    JPanel userPanel = new JPanel();
    JPanel linePanel = new JPanel();
    linePanel.setLayout(new BorderLayout());//BoxLayout(linePanel,BoxLayout.Y_AXIS));
    JPanel ellipsePanel  = new JPanel();
    ellipsePanel.setLayout(new BorderLayout());//(ellipsePanel,BoxLayout.Y_AXIS));
    userPanel.setLayout(new BoxLayout(userPanel,BoxLayout.X_AXIS));
    userPanel.add(linePanel);
    userPanel.add(ellipsePanel);

    JPanel geoLinePanel = new JPanel();
    geoLinePanel.setLayout(new SpringLayout());//GridLayout(8,2));
    JPanel geoEllipsePanel = new JPanel();
    JPanel lButtonPanel = new JPanel();
    JPanel eButtonPanel = new JPanel();


    linePanel.add(geoLinePanel,BorderLayout.CENTER);
    ellipsePanel.add(geoEllipsePanel,BorderLayout.CENTER);

    linePanel.add(lButtonPanel,BorderLayout.SOUTH);
    ellipsePanel.add(eButtonPanel,BorderLayout.SOUTH);



    geoEllipsePanel.setLayout(new SpringLayout());//GridLayout(8,2));
    geoEllipsePanel.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("GeoEllipse"),
            BorderFactory.createEmptyBorder(5,5,5,5)));

    JLabel labelCenterX = new JLabel("X1(degrees):");
    labelCenterX.setLabelFor(centerX1Text);
    centerX1Text = new JTextField();

    JLabel labelCenterY = new JLabel("Y1(degrees):");
    labelCenterY.setLabelFor(centerY1Text);
    centerY1Text = new JTextField();

    JLabel labelXAxis = new JLabel("X Axis(km):");
    labelXAxis.setLabelFor(XAxisText);
    XAxisText = new JTextField();

    JLabel labelYAxis = new JLabel("Y Axis(km):");
    labelYAxis.setLabelFor(YAxisText);
    YAxisText = new JTextField();
    JLabel rotation = new JLabel("Rotation(degrees)");
    rotation.setLabelFor(rotationText);
    rotationText = new JTextField();





    ellipseButton  = new JButton("Draw Ellipse");
    kpButton = new JButton("Draw key points");
    clearButton  = new JButton("Clear Fields");

    eButtonPanel.add(ellipseButton);
    eButtonPanel.add(kpButton);
    eButtonPanel.add(clearButton);

    ellipseButton.addActionListener(this);
    kpButton.addActionListener(this);
    clearButton.addActionListener(this);


    ellipseButton.setMnemonic(KeyEvent.VK_E);
    kpButton.setMnemonic(KeyEvent.VK_K);
    clearButton.setMnemonic(KeyEvent.VK_C);

    geoEllipsePanel.add(labelCenterX);
    geoEllipsePanel.add(centerX1Text);
    geoEllipsePanel.add(labelCenterY);
    geoEllipsePanel.add(centerY1Text);

    geoEllipsePanel.add(labelXAxis);
    geoEllipsePanel.add(XAxisText);

    geoEllipsePanel.add(labelYAxis);
    geoEllipsePanel.add(YAxisText);
    geoEllipsePanel.add(rotation);
    geoEllipsePanel.add(rotationText);


    SpringUtilities.makeCompactGrid(geoEllipsePanel,
        5, 2, //rows, cols
        6, 6,        //initX, initY
        6, 6);       //xPad, yPad



    clearLineButton = new JButton("Add Layer");



    computeButton = new JButton("Draw GeoLine");
    computeButton.addActionListener(this);
    lButtonPanel.add(computeButton);
    refreshButton = new JButton("Clear Fields");
    refreshButton.addActionListener(this);
    lButtonPanel.add(refreshButton);

    computeButton.setMnemonic(KeyEvent.VK_C);
    refreshButton.setMnemonic(KeyEvent.VK_R);
    clearLineButton.setMnemonic(KeyEvent.VK_A);



    geoLinePanel.setName("Input");


    JLabel labelX = new JLabel("X1:");
    labelX.setLabelFor(X1Text);
    X1Text = new JTextField();
    JLabel labelY = new JLabel("Y1:");
    labelY.setLabelFor(Y1Text);
    Y1Text = new JTextField();
    JLabel X2Label = new JLabel("X2:");
    X2Label.setLabelFor(X2Text);
    X2Text = new JTextField();
    JLabel Y2Label = new JLabel("Y2:");
    Y2Label.setLabelFor(Y2Text);
    Y2Text = new JTextField();
    lineCbo = new JComboBox();
    lineCbo.addItem(R);
    lineCbo.addItem(G);
    lineCbo.addItem(C);
    lineCbo.addActionListener(this);
    JLabel lineLabel = new JLabel("Line Type:");
    lineLabel.setLabelFor(lineCbo);
    JLabel labelAzi = new JLabel("Azimuth(degree):");
    labelAzi.setLabelFor(aziText);
    aziText = new JTextField();
    JLabel labelDist = new JLabel("Distance(km) :");
    labelDist.setLabelFor(distText);
    distText = new JTextField();

    geoLinePanel.add(labelX);
    geoLinePanel.add(X1Text);
    geoLinePanel.add(labelY);
    geoLinePanel.add(Y1Text);
    geoLinePanel.add(X2Label);
    geoLinePanel.add(X2Text);
    geoLinePanel.add(Y2Label);
    geoLinePanel.add(Y2Text);
    geoLinePanel.add(lineLabel);
    geoLinePanel.add(lineCbo);
    geoLinePanel.add(labelAzi);
    geoLinePanel.add(aziText);
    geoLinePanel.add(labelDist);
    geoLinePanel.add(distText);

    SpringUtilities.makeCompactGrid(geoLinePanel,
        7, 2, //rows, cols
        6, 6,        //initX, initY
        6, 6);       //xPad, yPad


    geoLinePanel.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("GeoPolyline"),
            BorderFactory.createEmptyBorder(5,5,5,5)));

    //geoLinePanel.add(lButtonPanel);


    //linePanel.add(geoLinePanel,BorderLayout.WEST);
    //linePanel.add(lButtonPanel,BorderLayout.SOUTH);

    try {
      toolBarControl.setBuddyControl(mapControl);
      toolBarControl.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //open
      toolBarControl.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomIn
      toolBarControl.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomOut
      toolBarControl.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //Pan
      toolBarControl.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleIconAndText);
      toolBarControl.addItem(new com.esri.arcgis.controls.ControlsSelectTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText);
    }
    catch (Exception ex) {

    }


    mainPanel.add(toolBarControl,BorderLayout.NORTH);
    mainPanel.add(mapControl,BorderLayout.CENTER);
    mainPanel.add(userPanel,BorderLayout.SOUTH);
    getContentPane().add(mainPanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  public boolean getPoints()
  {

    try {
      double x1 = java.lang.Double.valueOf(X1Text.getText()).doubleValue();
      double y1 = java.lang.Double.valueOf(Y1Text.getText()).doubleValue();
      double x2 = java.lang.Double.valueOf(X2Text.getText()).doubleValue();
      double y2 = java.lang.Double.valueOf(Y2Text.getText()).doubleValue();
      startPoint = new Point();
      startPoint.putCoords(x1,y1);
      endPoint = new Point();
      endPoint.putCoords(x2,y2);
      return true;
    }
    catch (Exception ex) {
      JOptionPane.showMessageDialog(this,"Input value incorrect");
    }

    return false;
  }

  public void actionPerformed(java.awt.event.ActionEvent e)
  {
    if(e.getSource()==computeButton){
      if(((String)lineCbo.getSelectedItem()).equalsIgnoreCase(G)){
        if(getPoints())

          drawGeodesicLine(startPoint,endPoint);


      }
      else if(((String)lineCbo.getSelectedItem()).equalsIgnoreCase(R)){
        if(getPoints())

          drawRhumbLine(startPoint, endPoint);
      }

      else if(((String)lineCbo.getSelectedItem()).equalsIgnoreCase(C)){
        if(getPoints())

          drawGreateCircle(startPoint, endPoint);

      }
    }

    else if(e.getSource()==ellipseButton)
    {
      if(checkInput())
        drawEllipse();
    }


    else if(e.getSource()==clearButton)
    {
      try {
        mapControl.getActiveView().getGraphicsContainer().deleteAllElements();
        mapControl.refresh(esriViewDrawPhase.esriViewBackground, null, null);
        flag = false;

      }
      catch (Exception ex) {
        System.out.println("Error in refresh");
        ex.printStackTrace();
      }

      centerX1Text.setText("");
      centerY1Text.setText("");
      XAxisText.setText("");
      YAxisText.setText("") ;
      rotationText.setText("");
    }

    else if(e.getSource()==kpButton)
    {
      if(checkInput())
        drawKeyPoints();
    }

    else if(e.getSource()==refreshButton){
      try {
        mapControl.getActiveView().getGraphicsContainer().deleteAllElements();
        mapControl.refresh(esriViewDrawPhase.esriViewBackground, null, null);
        pointVector.removeAllElements();

      }
      catch (Exception ex) {
        System.out.println("Error in refresh");
        ex.printStackTrace();
      }

      X1Text.setText("");
      Y1Text.setText("") ;
      X2Text.setText("") ;
      Y2Text.setText("") ;
    }

  }




  public void drawKeyPoints()
  {
    try {
      //outline color
      IRgbColor color = new RgbColor();
      color.setGreen(255);

      //make fill color null
      IColor nullColor = new RgbColor();
      nullColor.setNullColor(true);

      //create the outline symbol
      ILineSymbol lineSymbol = new SimpleLineSymbol();
      lineSymbol.setColor(color);
      lineSymbol.setWidth(1.5);
      IHashLineSymbol hashLineSymbol = new HashLineSymbol();
      hashLineSymbol.setHashSymbolByRef(lineSymbol);

      //create the fill symbol for the polygon
      IFillSymbol fillSymbol  = new SimpleFillSymbol();
      fillSymbol.setOutline(hashLineSymbol);
      fillSymbol.setColor(nullColor);

      //Run the CreateGeoEllipse subprocedure to get the geometry of the
      //key points polygon generated from the GeoEllipse.

      if(keypointPolygon == null)
        createEllipse();
      //create the IElement to be rendered  a PolygonElement and
      // its geometry to the key points polygon geometry defined in the
      //CreateGeoEllipse subprocedure
      IElement keyPointElement = new PolygonElement();
      keyPointElement.setGeometry(keypointPolygon);

      //QI to IFillShapeElement to  the symbology of the key points polygon graphic
      IFillShapeElement fillElement  ;
      fillElement   = (IFillShapeElement)keyPointElement;
      fillElement.setSymbol(fillSymbol);
      IElement e = (IElement)fillElement;

      //define the graphics container and draw the key points polygon graphic
      mapControl.getActiveView().getGraphicsContainer().addElement(e,0);
      mapControl.getActiveView().refresh();



    }
    catch (Exception ex) {

    }



  }




  public boolean checkInput()
  {

    if(centerX1Text.getText().length()==0)
    {
      JOptionPane.showMessageDialog(this,"Input center X value","Error Message",JOptionPane.ERROR_MESSAGE);
      return false;
    }
    else if(centerY1Text.getText().length()==0)
    {
      JOptionPane.showMessageDialog(this,"Input center Y value","Error Message",JOptionPane.ERROR_MESSAGE);
      return false;
    }

    else if(XAxisText.getText().length()==0)
    {
      JOptionPane.showMessageDialog(this,"Input X axis value","Error Message",JOptionPane.ERROR_MESSAGE);
      return false;
    }

    else if(YAxisText.getText().length()==0)
    {
      JOptionPane.showMessageDialog(this,"Input X axis value","Error Message",JOptionPane.ERROR_MESSAGE);
      return false;
    }

    else if(rotationText.getText().length()==0)
    {
      JOptionPane.showMessageDialog(this,"Input rotation value","Error Message",JOptionPane.ERROR_MESSAGE);
      return false;
    }



    return true;
  }


  public IGeoEllipse createEllipse()
  {
    IGeoEllipse gEllipse = null;
    try {
      //Get axis values in km
      double x = java.lang.Double.parseDouble(XAxisText.getText());
      double y = java.lang.Double.parseDouble(YAxisText.getText());

      //Get ellipse origin values
      double xCenter = java.lang.Double.parseDouble(centerX1Text.getText());
      double yCenter = java.lang.Double.parseDouble(centerY1Text.getText());

      //Create origin point for ellipse
      IPoint startPoint = new Point();
      startPoint.putCoords(xCenter,yCenter);

      //Create new ellipse with origin point
      //both axis in km and rotation
      gEllipse = new GeoEllipse();
      gEllipse.setOrigin(startPoint);
      gEllipse.setAxisX(x*1000);
      gEllipse.setAxisY(y*1000);

      //set rotation in degrees
      gEllipse.setRotation(java.lang.Double.parseDouble(rotationText.getText()));

      //set the number of vertices to be used in the creation
      //of the polygon representation of the geoellipse
      gEllipse.setGeoEllipsePointCount(100);

      //define the key points polygon, which is a polygon connecting the end
      //points of the geoellipse X and Y axes
      keypointPolygon = gEllipse.getKeyPoints();

    }
    catch (Exception ex) {

    }

    return gEllipse;

  }

  private void drawEllipse(){

    try {
      //Outline color
      IRgbColor r = new RgbColor();
      r.setRed(255);

      //Fill color
      IColor noFill = new RgbColor();
      noFill.setNullColor(true);

      //Create GeoEllipse
      IGeoEllipse gEllipse = createEllipse();
      //Create polygon with geo ellipse
      IPolygon poly = (IPolygon)(gEllipse);


      IElement gElement = new GeoEllipseElement();

      //Create line symbol
      ILineSymbol lineSymbol = new SimpleLineSymbol();
      lineSymbol.setColor(r);
      lineSymbol.setWidth(1.5);

      //Create fill symbol
      IFillSymbol fillSymbol = new LineFillSymbol();
      fillSymbol.setOutline(lineSymbol);
      fillSymbol.setColor(noFill);

      //Set the geometry of the ellipse element
      gElement.setGeometry(poly);

      //Create ielement
      IFillShapeElement fillElement = (IFillShapeElement) gElement;
      fillElement.setSymbol(fillSymbol);
      IElement ee = (IElement)(fillElement);
      mapControl.getActiveView().getGraphicsContainer().addElement(ee,0);
      mapControl.getActiveView().refresh();


    }
    catch (Exception ex) {
      JOptionPane.showMessageDialog(this, "Geol Ellipse not created ", "Error", JOptionPane.ERROR_MESSAGE);

    }




  }








  private void drawRhumbLine(IPoint p1, IPoint p2){

    try {
      //Set up measurement tool with
      //end point start point and line type
      MeasurementTool measureTool = new MeasurementTool();
      measureTool.setFromPoint(p1);
      measureTool.setToPoint(p2);
      measureTool.setSpecialGeolineType(cjmtkSGType.cjmtkSGTRhumbLine);

      //Get poly line from measurement tool with desired number of points
      IPolyline polyLine = measureTool.getPolyline(100);

      //Set up geo polyline
      GeoPolyline g = new GeoPolyline();
      g.setPolyline(polyLine);

      //Set up outline color
      IRgbColor r = new RgbColor();
      r.setRGB(255);

      //Add outline color to the symbol which will
      //be added to the line
      IMarkerSymbol arrowMarker = new ArrowMarkerSymbol();
      arrowMarker.setColor(r);

      //Create simple line decoration element
      ISimpleLineDecorationElement simpleLineDecoElement = new SimpleLineDecorationElement();
      simpleLineDecoElement.setMarkerSymbol(arrowMarker);
      simpleLineDecoElement.addPosition(1.0);
      ILineDecorationElement lineDecoElement = simpleLineDecoElement;

      //Add line decoration element to line decoration
      ILineDecoration lineDecoration = new LineDecoration();
      lineDecoration.addElement(lineDecoElement);

      //Define carto line symbol
      ICartographicLineSymbol cartoLineSymbol = new CartographicLineSymbol();
      cartoLineSymbol.setColor(r);
      cartoLineSymbol.setWidth(1.5);

      //Set line properties
      ILineProperties lineProps = new CartographicLineSymbol();
      lineProps = (ILineProperties)(cartoLineSymbol);
      lineProps.setLineDecorationByRef(lineDecoration);

      //create line symbol from line properties
      ILineSymbol symbol = (ILineSymbol)(lineProps);

      //Set line elment with the symbol
      ILineElement lineElement = new LineElement();
      lineElement.setSymbol(symbol);

      //Set elemenet geometry with that of polyline
      IElement element = (IElement)lineElement;
      element.setGeometry(polyLine);

      //add line to graphics container
      mapControl.getActiveView().getGraphicsContainer().addElement(element,0);
      mapControl.getActiveView().refresh();

      distText.setBackground(Color.lightGray);
      aziText.setBackground(Color.lightGray);
      distText.setText(java.lang.String.valueOf(Math.round(measureTool.getDistance() / 1000)));
      aziText.setText(java.lang.String.valueOf(Math.round(measureTool.getAngle())));

    }


    catch (Exception ex) {
      ex.printStackTrace();
    }

  }


  /**
   * This method allows the user to draw a Geodesic Line
   * based on the inputs in the GUI
   * @param p1 - start point
   * @param p2 - end point
   */
  private void drawGeodesicLine(IPoint p1, IPoint p2){

    try {

      //Set measure tool with
      //end point start point and line type
      MeasurementTool measureTool = new MeasurementTool();
      measureTool.setFromPoint(p1);
      measureTool.setToPoint(p2);
      measureTool.setSpecialGeolineType(cjmtkSGType.cjmtkSGTGeodesic);

      //Get polyline from the measure tool with the number of points desired
      IPolyline polyLine = measureTool.getPolyline(100);

      //Set up geopolyline
      GeoPolyline g = new GeoPolyline();
      g.setPolyline(polyLine);

      //Outer boundary color
      IRgbColor r = new RgbColor();
      r.setRGB(255);
      IMarkerSymbol arrowMarker = new ArrowMarkerSymbol();
      arrowMarker.setColor(r);

      //Create simple line decoration element
      ISimpleLineDecorationElement simpleLineDecoElement = new SimpleLineDecorationElement();
      simpleLineDecoElement.setMarkerSymbol(arrowMarker);
      simpleLineDecoElement.addPosition(1.0);
      ILineDecorationElement lineDecoElement = simpleLineDecoElement;

      //Add line decoration element to line decoration
      ILineDecoration lineDecoration = new LineDecoration();
      lineDecoration.addElement(lineDecoElement);

      //Define carto line symbol
      ICartographicLineSymbol cartoLineSymbol = new CartographicLineSymbol();
      cartoLineSymbol.setColor(r);
      cartoLineSymbol.setWidth(1.5);

      //Set line properties
      ILineProperties lineProps = new CartographicLineSymbol();
      lineProps = (ILineProperties)(cartoLineSymbol);
      lineProps.setLineDecorationByRef(lineDecoration);

      ILineSymbol symbol = (ILineSymbol)(lineProps);
      ILineElement lineElement = new LineElement();
      lineElement.setSymbol(symbol);
      IElement element = (IElement)lineElement;
      element.setGeometry(polyLine);

      mapControl.getActiveView().getGraphicsContainer().addElement(element,0);
      mapControl.getActiveView().refresh();

      distText.setBackground(Color.lightGray);
      aziText.setBackground(Color.lightGray);
      distText.setText(java.lang.String.valueOf(Math.round(measureTool.getDistance() / 1000)));
      aziText.setText(java.lang.String.valueOf(Math.round(measureTool.getAngle())));


    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }


  /**
   * This method allows the user to draw a Great Circle
   * based on the inputs in GUI
   * @param p1  start point
   * @param p2  end point
   */

  private void drawGreateCircle(IPoint p1, IPoint p2){
    try{

      //Set up measurement tool
      //with start point end point and line type
      MeasurementTool measureTool = new MeasurementTool();
      measureTool.setFromPoint(p1);
      measureTool.setToPoint(p2);
      measureTool.setSpecialGeolineType(cjmtkSGType.cjmtkSGTGreatCircle);

      //get poly line from measurement tool with number of points desired
      IPolyline polyLine = measureTool.getPolyline(100);

      //Set up geo polyline
      GeoPolyline g = new GeoPolyline();
      g.setPolyline(polyLine);

      //Set up outline color
      IRgbColor r = new RgbColor();
      r.setRGB(255);

      IMarkerSymbol arrowMarker = new ArrowMarkerSymbol();
      arrowMarker.setColor(r);

      //Create simple line decoration element
      ISimpleLineDecorationElement simpleLineDecoElement = new SimpleLineDecorationElement();
      simpleLineDecoElement.setMarkerSymbol(arrowMarker);
      simpleLineDecoElement.addPosition(1.0);
      ILineDecorationElement lineDecoElement = simpleLineDecoElement;

      //Add line decoration element to line decoration
      ILineDecoration lineDecoration = new LineDecoration();
      lineDecoration.addElement(lineDecoElement);

      //Define carto line symbol
      ICartographicLineSymbol cartoLineSymbol = new CartographicLineSymbol();
      cartoLineSymbol.setColor(r);
      cartoLineSymbol.setWidth(1.5);

      //Set line properties
      ILineProperties lineProps = new CartographicLineSymbol();
      lineProps = (ILineProperties)(cartoLineSymbol);
      lineProps.setLineDecorationByRef(lineDecoration);

      ILineSymbol symbol = (ILineSymbol)(lineProps);
      ILineElement lineElement = new LineElement();
      lineElement.setSymbol(symbol);
      IElement element = (IElement)lineElement;
      element.setGeometry(polyLine);

      mapControl.getActiveView().getGraphicsContainer().addElement(element,0);
      mapControl.getActiveView().refresh();

      distText.setBackground(Color.lightGray);
      aziText.setBackground(Color.lightGray);
      distText.setText(java.lang.String.valueOf(Math.round(measureTool.getDistance() / 1000)));
      aziText.setText(java.lang.String.valueOf(Math.round(measureTool.getAngle())));

    }


    catch (Exception ex) {
      ex.printStackTrace();
    }
  }









  public void onAfterDraw(IMapControlEvents2OnAfterDrawEvent theEvent) {}
  public void onAfterScreenDraw(IMapControlEvents2OnAfterScreenDrawEvent theEvent) {}

  public void onBeforeScreenDraw(IMapControlEvents2OnBeforeScreenDrawEvent theEvent) {}

  public void onDoubleClick(IMapControlEvents2OnDoubleClickEvent theEvent) {

  }

  public void onExtentUpdated(IMapControlEvents2OnExtentUpdatedEvent theEvent) {}

  public void onFullExtentUpdated(IMapControlEvents2OnFullExtentUpdatedEvent theEvent) {}

  public void onKeyDown(IMapControlEvents2OnKeyDownEvent theEvent) {}

  public void onKeyUp(IMapControlEvents2OnKeyUpEvent theEvent) {}

  public void onMapReplaced(IMapControlEvents2OnMapReplacedEvent theEvent) {}

  public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent) {

    try {

      //On a right mouse button click use the TrackPolygon method on the map control
      //to define the vertices of an eventual GeoPolygon. Each right mouse button
      //click adds a vertex to the polygon; double-clicking the right mouse button
      //completes the polygon.
      int button= theEvent.getButton();
      if(button==2){

        //Create the polygon from the TrackPolygon method. This polygon will be
        //the foundation for a GeoPolygon object.
        IPolygon    trackPoly ;
        IElement trackPolyElement  ;
        trackPoly = (IPolygon)mapControl.trackPolygon();

        //Define the GeoPolygon, QI to IGeoPolygon
        IGeoPolygon geoPolygon = new GeoPolygon();
        geoPolygon.setPolygon(trackPoly);

        // the GeoPolyline type to form the segments of the GeoPolygon. In this ce
        //the segments will be Geodesic lines.
        geoPolygon.setSpecialGeolineType(com.esri.arcgis.defensesolutions.cjmtkSGType.cjmtkSGTGeodesic);

        // the GeoPolygon spatial reference.
        //  geoPolygon.BeSpatialReference = SpatialRef

        //outline color
        IRgbColor color = new RgbColor();
        color.setBlue(255);

        //make fill color null
        IColor nullColor  = new RgbColor();
        nullColor.setNullColor(true);

        //create the outline symbol
        ILineSymbol lineSymbol  =  new SimpleLineSymbol();
        lineSymbol.setColor(color);
        lineSymbol.setWidth(1.5);

        //create the fill symbol for the GeoPolygon
        IFillSymbol fillSymbol = new SimpleFillSymbol();
        fillSymbol.setOutline(lineSymbol);
        fillSymbol.setColor(nullColor);

        //Create the IElement to be rendered  a GeoPolygonElement and
        // its geometry to the GeoPolygon geometry. When a GeoPolygonElement
        //graphic is moved from one location to another in the map control
        //the GeoPolygon geometry is automatically updated bed on the
        //geographic location and the graphic is updated accordingly.
        trackPolyElement = new GeoPolygonElement();
        trackPolyElement.setGeometry((IGeometry)geoPolygon);

        //QI to IFillShapeElement to  the symbology of the GeoPolygon graphic
        IFillShapeElement fillElement ;
        fillElement = (IFillShapeElement)trackPolyElement;
        fillElement.setSymbol(fillSymbol);


        IGraphicsContainer gCont = mapControl.getActiveView().getGraphicsContainer();
        gCont.deleteAllElements();
        IElement element = (IElement)fillElement;
        //element.setGeometry(pointOnMap);
        gCont.addElement(element,0);

        //add the point to the map and refresh the map
        mapControl.refresh(esriViewDrawPhase.esriViewGraphics,null,null);

      }
    }
    catch (Exception ex) {
    }
  }

  public void onMouseMove(IMapControlEvents2OnMouseMoveEvent theEvent) {}

  public void onMouseUp(IMapControlEvents2OnMouseUpEvent theEvent) {}

  public void onOleDrop(IMapControlEvents2OnOleDropEvent theEvent) {}

  public void onSelectionChanged(IMapControlEvents2OnSelectionChangedEvent theEvent) {}

  public void onViewRefreshed(IMapControlEvents2OnViewRefreshedEvent theEvent) {}

  public void onMouseUp(ITOCControlEventsOnMouseUpEvent theEvent) {}

  public static void main(String[] args)
  {
    String arcgisHome = null;
    arcgisHome = System.getenv("AGSDEVKITJAVA");
    if (arcgisHome == null) {
      JOptionPane.showInputDialog( "AGSDEVKITJAVA environment variable is not set, exiting application");
      System.exit( -1);
    }

    try {
      EngineInitializer.initializeVisualBeans();
      AoInitialize aoInit = new AoInitialize();

      // Check if license is valid
      if (aoInit.isProductCodeAvailable( esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeEngine);
      else {
        JOptionPane.showInputDialog( "Could not initialize an ArcGIS Engine license. Exiting application.");
        System.exit(-1);
      }

      new MAGeodesy( arcgisHome);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}