arcgissamples\globe\SunPositionTool.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Globe sun position
arcgissamples\globe\SunPositionTool.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 com.esri.arcgis.analyst3d.ISceneViewer;
import com.esri.arcgis.controls.BaseTool;
import com.esri.arcgis.controls.GlobeHookHelper;
import com.esri.arcgis.globecore.GlobeCamera;
import com.esri.arcgis.globecore.GlobeDisplay;

class SunPositionTool extends BaseTool {
  private static final long serialVersionUID = 1L;
  GlobeHookHelper globeHookHelper;
  GlobeCamera globeCamera;
  GlobeDisplay globeDisplay;
  ISceneViewer sceneViwer;
  boolean isTrackingSun;

  public SunPositionTool() {
    this.caption = "Set Sun Position";
    this.category = "Java Samples";
    this.message = "set sun position tool";
    this.name = "Java Samples Sun Position";
    this.toolTip = "Set Sun Position Tool";
    this.cursorPath = "SunPositionTool.cur";
    this.enabled = true;
  }

  /**
   * When this tool is created, initialize the hook helper and get the hook.
   */
  public void onCreate(Object hook) {
    try {
      if (this.globeHookHelper == null) {
        this.globeHookHelper = new GlobeHookHelper();
      }
      this.globeHookHelper.setHookByRef(hook);
    } catch (Exception e) {
      System.out.println("Couldn't create the SunPosition tool");
      e.printStackTrace();
      System.exit(1);
    }
  }

  /**
   * When this tool is clicked, set up the lighting.
   */
  public void onClick() {
    try {
      this.globeCamera = (GlobeCamera) this.globeHookHelper.getCamera();
      this.globeDisplay = (GlobeDisplay) this.globeHookHelper.getGlobeDisplay();
      //
      // Enable the light source and set light characteristics
      //
      this.globeDisplay.setIsSunEnabled(true);
      this.globeDisplay.setAmbientLight(0.0005f);
      this.globeDisplay.setSunContrast(90);
      //
      // Update the scene viewer.
      //
      this.sceneViwer = this.globeHookHelper.getActiveViewer();
    } catch (Exception e) {
      System.out.println("Couldn't enable the sun's light");
      e.printStackTrace();
      System.exit(1);
    }
  }
  /**
   * On initial mouse down, get it's position and from it set the sun's position.
   */
  public void onMouseDown(int Button, int Shift, int X, int Y) {
    this.isTrackingSun = true;
    try {
      double[] lat = {0.0};
      double[] lon = {0.0};
      double[] alt = {0.0};
      System.out.println("X is: " + X);
      System.out.println("Y is: " + Y);
      this.globeCamera.windowToGeographic(this.globeDisplay, this.sceneViwer, X, Y, false, lon, lat, alt);
      this.globeDisplay.setSunPosition(lat[0], lon[0]);
      this.sceneViwer.redraw(false);
    } catch (Exception e) {
      //
      // An exception is thrown if mouse is clicked outside of the globe itself
      // We ignore that case.
      //
    }
  }
  /**
   * Track mouse movement, updating the sun's position.
   */
  public void onMouseMove(int Button, int Shift, int X, int Y) {
    //
    // Only track movement if a mouse button is down.
    //
    if (!this.isTrackingSun) {
      return;
    }
    try {
      double[] lat = {0.0};
      double[] lon = {0.0};
      double[] alt = {0.0};
      this.globeCamera.windowToGeographic(this.globeDisplay, this.sceneViwer, X, Y, false, lon, lat, alt);
      this.globeDisplay.setSunPosition(lat[0], lon[0]);
      this.sceneViwer.redraw(false);
    } catch (Exception e) {
      //
      // An exception is thrown if mouse is moved outside of the globe itself
      // while the mouse button is down.  We ignore that case.
      //
    }
  }
  /**
   * Stop tracking the sun when the mouse button is released.
   */
  public void onMouseUp(int Button, int Shift, int X, int Y) {
    this.isTrackingSun = false;
  }
}