arcgissamples\graphicstracker\AddPointGTCommand.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Point GraphicsTracker
arcgissamples\graphicstracker\AddPointGTCommand.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.graphicstracker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.Timer;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IMap;
import com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.display.IDisplayTransformation;
import com.esri.arcgis.display.IScreenDisplay;
import com.esri.arcgis.enginecore.GraphicTracker;
import com.esri.arcgis.enginecore.IGraphicTracker;
import com.esri.arcgis.enginecore.IGraphicTrackerSymbol;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.system._WKSPoint;


public class AddPointGTCommand extends BaseCommand {
  
  private static final long serialVersionUID = 1L;
  private HookHelper hookHelper = null;
  private IGraphicTracker graphicTracker;
  private IGraphicTrackerSymbol graphicTrackerSymbol;
  private IActiveView activeView;
  private IScreenDisplay screenDisplay;
  private IDisplayTransformation displayTransformation;
  private IEnvelope mapVisibleExtent;
  private IMap map;
  private _WKSPoint[] wksPoints;
  private int numOfPoints = 10; // number of points
  private List<PointData> points;
  //private Random r;
  private Timer timer;
  private ActionListener task = null;
  
  public AddPointGTCommand() throws Exception{
    name = "Add GraphicTracker";
    caption = "Add GraphicTracker";
    toolTip = "Add GraphicTracker";
    message = "Add GraphicTracker";
    category = "CustomCommand"; 
    enabled = true;
  }
  
  public void onCreate(Object obj) {
    try {
      hookHelper = new HookHelper();
      hookHelper.setHookByRef( obj );


    } catch (IOException ex) {
      ex.printStackTrace();
      throw new RuntimeException(ex);
    }
   }
  
  public void onClick() {
    try{
      System.out.println("Add GraphicTracker is Clicked");
      
            points = new ArrayList<PointData>(numOfPoints);

            map = hookHelper.getFocusMap();
            activeView = hookHelper.getActiveView();
            screenDisplay = activeView.getScreenDisplay();
            displayTransformation = screenDisplay.getDisplayTransformation();

            if (graphicTracker == null)
                graphicTracker = new GraphicTracker();

            if (map != null)
                graphicTracker.initialize(map);

      InitData(98);
            
        //Create a timer and attach a listener that would fire events every 30ms.    
        task = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
          try {
            UpdatePoints();
          }
          catch (Exception e) {
          e.printStackTrace();
            }
        }
          };
          timer = new Timer(30,task);
          timer.start();
            
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
  
    private void InitData(int charIndex) throws Exception
    {
      wksPoints = new _WKSPoint[numOfPoints];
         
        ISpatialReference spatialReference;
        spatialReference = hookHelper.getFocusMap().getSpatialReference();

        if (displayTransformation != null)
        {
            mapVisibleExtent = displayTransformation.getFittedBounds();
        }
        else
        {
            if (mapVisibleExtent == null)
            {
                mapVisibleExtent = new Envelope();
                mapVisibleExtent.putCoords(-130.0, 25.0, -70.0, 55.0);
                mapVisibleExtent.setSpatialReferenceByRef(spatialReference);
            }
        }

        // initialize the points data according to the visible extent
        double[] XMin = new double[1];
        double[] YMin = new double[1];
        double[] XMax = new double[1];
        double[] YMax = new double[1];
        
        mapVisibleExtent.queryCoords(XMin, YMin, XMax, YMax);
        double dWidth = XMax[0] - XMin[0];
        double dHeight = YMax[0] - YMin[0];
        double dStepX = dWidth / 50;
        double dStepY = dHeight / 50;
        
        String devKitHome = System.getenv("AGSDEVKITJAVA");
    
        String bitmapPath = devKitHome + "java" + File.separator + "samples"
                         + File.separator + "arcobjects" + File.separator + "display"
                         + File.separator + "graphicstracker" + File.separator + "graphicstrackerpoint"
                         + File.separator + "image" + File.separator + "PoliceCar32.PNG";
        
        //Test to make sure bitmap is present in relative location
        File bitmapFile = new File(bitmapPath);
        
        if(!bitmapFile.exists()){
            System.err.println("The PoliceCar32.PNG was not found in the following locaiton: " + bitmapFile.getParent());
            System.err.println("Verify that PoliceCar32.PNG can be located in the specified folder.");
            System.err.println("Exiting execution of this sample...");
            System.exit(-1);
        }
      
        graphicTrackerSymbol = graphicTracker.createSymbolFromPath(bitmapPath, "");

        // suspend auto updates in order to do a bulk of updates
        graphicTracker.setSuspendUpdate(true);

        Random rand = new Random();

        for (int i = 0; i < numOfPoints; i++)
        {
          wksPoints[i] = new _WKSPoint();
          
          wksPoints[i].x = XMin[0] + rand.nextDouble() * dWidth;
            wksPoints[i].y = YMin[0] + rand.nextDouble() * dHeight;

            // add the point data
            double dRand = rand.nextDouble();
            PointData pointData = new PointData();
            pointData.ID = i;
            pointData.pPoint = wksPoints[i];
            pointData.dStepX = (dRand < 0.5 ? 1.0 : -1.0) * dRand * dStepX;
            pointData.dStepY = (dRand < 0.5 ? 1.0 : -1.0) * dRand * dStepY;

            IPoint point = new Point();
            point.putCoords(wksPoints[i].x, wksPoints[i].y);
            point.setSpatialReferenceByRef(spatialReference);

            // need to add the new element to the graphic tracker
            pointData.trackerID = graphicTracker.add(point, graphicTrackerSymbol);

            graphicTracker.setTransparency(pointData.trackerID, 20);
            points.add(pointData);
        }

        graphicTracker.setSuspendUpdate(false);
    }
    
    class PointData
    {
        public int trackerID;
        public int ID;
        public double dStepX;
        public double dStepY;
        public _WKSPoint pPoint;
    }
    
    private void UpdatePoints() throws Exception
    {
        // initialize the points data according to the visible extent
        double[] XMin = new double[1];
        double[] YMin = new double[1];
        double[] XMax = new double[1];
        double[] YMax = new double[1];
        
        mapVisibleExtent.queryCoords(XMin,YMin,XMax,YMax);

        // suspend auto updates in order to do a bulk of updates
        graphicTracker.setSuspendUpdate(true);

        for (int i = 0; i < points.size(); i++)
        {
            PointData point = points.get(i);

            point.pPoint.x += point.dStepX;
            point.pPoint.y += point.dStepY;

            //test that the item's location is within the fitted bounds
            if (point.pPoint.x > XMax[0]) point.dStepX = -Math.abs(point.dStepX);
            if (point.pPoint.x < XMin[0]) point.dStepX = Math.abs(point.dStepX);
            if (point.pPoint.y > YMax[0]) point.dStepY = -Math.abs(point.dStepY);
            if (point.pPoint.y < YMin[0]) point.dStepY = Math.abs(point.dStepY);

            double heading = (360.0 + 90.0 - Math.atan2(point.dStepY, point.dStepX) * 180 / Math.PI);

            if (heading > 360.0)
                heading -= 360.0;

            graphicTracker.moveTo(point.trackerID, point.pPoint.x, point.pPoint.y, 1000);
            points.set(i,point);
        }

       graphicTracker.setSuspendUpdate(false);
    }  
}