arcgissamples\globe\AnimationRunnable.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Camera and layer animation in the Globe control
arcgissamples\globe\AnimationRunnable.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.globe;

import javax.swing.JOptionPane;

import com.esri.arcgis.analyst3d.AnimationTrack;
import com.esri.arcgis.analyst3d.IAnimationTrack;
import com.esri.arcgis.analyst3d.IAnimationTracks;
import com.esri.arcgis.analyst3d.IAnimationType;
import com.esri.arcgis.analyst3d.IKeyframe;
import com.esri.arcgis.analyst3d.IScene;
import com.esri.arcgis.analyst3d.IViewers3D;
import com.esri.arcgis.beans.globe.GlobeBean;
import com.esri.arcgis.globecore.AnimationTypeGlobeCamera;
import com.esri.arcgis.globecore.AnimationTypeGlobeLayer;
import com.esri.arcgis.globecore.GlobeCameraKeyframe;
import com.esri.arcgis.globecore.IGlobeDisplay;
import com.esri.arcgis.system.Array;
import com.esri.arcgis.system.IArray;

public class AnimationRunnable implements Runnable {
  private GlobeBean globeBean;

  private AnimationPanel animationPanel;

  public AnimationRunnable(GlobeBean globeBean, AnimationPanel animationPanel) {
  super();
  this.globeBean = globeBean;
  this.animationPanel = animationPanel;
  }

  public void run() {
  //Play the animation based on the user settings
  try {
    IAnimationTracks tracks = (IAnimationTracks) this.globeBean.getGlobe();
    IViewers3D viewers3D = this.globeBean.getGlobe().getGlobeDisplay();
    // exit if document doesnt contain any animation tracks
    if (tracks.getTrackCount() == 0) {
    JOptionPane
      .showMessageDialog(
        null,
        "The loaded globe document or animation file  do not contain any frames.",
        "Cannot play animation", JOptionPane.ERROR_MESSAGE);
    return;

    }
    if (this.animationPanel.getRadioDuration().isSelected())
    //play animation based on duration
    playAnimation(tracks, viewers3D);
    else if (this.animationPanel.getRadioIterations().isSelected())
    //play animation based on iterations
    playAnimationFast(tracks, viewers3D);

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

  private void playAnimation(IAnimationTracks tracks, IViewers3D viewers3D)
    throws Exception {
  int numCycles = new Integer(this.animationPanel.getTxtCycle().getText());
  int duration = new Integer(this.animationPanel.getTxtDuration().getText());
  for (int i = 1; i <= numCycles; i++) {
    long startTime = System.currentTimeMillis();
    double elapsedTime;
    do {
    elapsedTime = (System.currentTimeMillis() - startTime) / 1000.0;
    if (elapsedTime > duration)
      elapsedTime = duration;
    tracks.applyTracks(null, elapsedTime, duration);
    viewers3D.refreshViewers();

    } while (elapsedTime < duration);
  }

  }

  private void playAnimationFast(IAnimationTracks sceneTracks,
    IViewers3D viewers3D) throws Exception {

  IScene scene = (IScene) sceneTracks;
  IArray trackGlbArray = new Array();
  IArray trackLyrArray = new Array();

  IAnimationTrack track;
  IAnimationTrack trackLayer;
  IAnimationTrack trackGlobe = null;
  IAnimationType animType;
  // IAnimationType animLayer;
  IAnimationType animGlobeCam = null;
  IKeyframe kFGlbCam;
  // IKeyframe kFGlbLayer;
  int k;
  int[] count = new int[1000];

  // get each track from the scene and store traks of the same
  // kind in in an Array
  for (int i = 0; i <= sceneTracks.getTrackCount() - 1; i++) {
    track = (IAnimationTrack) sceneTracks.getTracks().getElement(i);
    k = i;
    animType = track.getAnimationType();
    if (animType instanceof AnimationTypeGlobeLayer)// "{7CCBA704-3933-4D7A-8E89-4DFEE88AA937}")
    {
    // GlobeLayer
    trackLayer = new AnimationTrack();
    trackLayer = track;
    trackLayer.setAnimationTypeByRef(animType);
    // kFGlbLayer = new GlobeLayerKeyframe();
    // animLayer = animType;
    // Store the keyframe count of each track in an array
    count[i] = trackLayer.getKeyframeCount();
    trackLyrArray.add(trackLayer);
    } else if (animType instanceof AnimationTypeGlobeCamera) // "{D4565495-E2F9-4D89-A8A7-D0B69FD7A424}")
    {
    // Globe Camera type
    trackGlobe = new AnimationTrack();
    trackGlobe = track;
    trackGlobe.setAnimationTypeByRef(animType);
    kFGlbCam = new GlobeCameraKeyframe();
    animGlobeCam = animType;
    // Store the keyframe count of each track in an array
    count[i] = trackGlobe.getKeyframeCount();
    trackGlbArray.add(trackGlobe);
    } else {
    JOptionPane.showMessageDialog(null, "Animation Type "
      + animType.getName()
      + " Not Supported. Check if the animation file is valid.",
      "Cannot play animation", JOptionPane.ERROR_MESSAGE);

    return;
    }

  }

  double time = 0;
  int keyFrameLayerCount;
  int keyFrameCount;
  int cycles = new Integer(this.animationPanel.getTxtCycle().getText());
  int iteration = new Integer(this.animationPanel.getTxtDuration().getText());
  for (int i = 1; i <= cycles; i++) // no of cycles...
  {
    for (double start = 0; start <= iteration; start++) // no of
    // iterations...
    {

    for (k = 0; k <= trackGlbArray.getCount() - 1; k++) {
      trackGlobe = (IAnimationTrack) trackGlbArray.getElement(k);
      if (trackGlobe != null) {
      if (time >= trackGlobe.getBeginTime()) {
        keyFrameCount = trackGlobe.getKeyframeCount();
        kFGlbCam = trackGlobe.getKeyframe(trackGlobe.getKeyframeCount()
          - keyFrameCount);
        // reset object
        animGlobeCam.resetObject(scene, kFGlbCam);
        // interpolate by using track
        trackGlobe.interpolateObjectProperties(scene, time);
        keyFrameCount = keyFrameCount - 1;
      }
      }
    }

    for (int t = 0; t <= trackLyrArray.getCount() - 1; t++) {
      trackLayer = (IAnimationTrack) trackLyrArray.getElement(t);
      if (trackLayer != null) {
      if (time >= trackLayer.getBeginTime()) {
        keyFrameLayerCount = trackLayer.getKeyframeCount();
        // kFGlbLayer = trackLayer.getKeyframe(trackLayer
        // .getKeyframeCount()
        // - keyFrameLayerCount);
        // interpolate by using track
        trackLayer.interpolateObjectProperties(scene, time);
        keyFrameLayerCount = keyFrameLayerCount - 1;
      }
      }
    }

    // reset interpolation Point
    time = start / iteration;
    // refresh the globeviewer(s)
    ((IGlobeDisplay) viewers3D).refreshViewers();
    }
  }
  }

}