arcgissamples\globe\SunPosition.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Globe sun position
arcgissamples\globe\SunPosition.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 java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import com.esri.arcgis.beans.globe.GlobeBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.controls.ControlsGlobeNavigateTool;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseExtensionCode;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.esriCommandStyles;

public class SunPosition extends JFrame {

  private static final long serialVersionUID = 1L;
  public static void main(String[] args) {
    System.out.println("Starting Sun Position - An ArcObjects Java SDK Developer Sample");
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    if (devKitHome == null || !(new File(devKitHome).exists())) {
      System.out.println(((devKitHome == null)?"AGSDEVKITJAVA environment variable":devKitHome)+" does not exist.\nExiting...");
      System.exit(-1);
    }
    //
    // Change the following line if you want to use different data, or just pass in a
    // different 3DD file.
    //
    String globeTexture = devKitHome + "java" + File.separator + 
                       "samples" + File.separator + 
                       "data" + File.separator + 
                       "globe_data" + File.separator +
                       "Default_Document.3dd";
    if (args.length > 0) {
      globeTexture = args[0];
    }
    File globeTextureFile = new File(globeTexture);
    if (!globeTextureFile.exists()) {
      System.out.println("globeTexture file does not exist: " + globeTextureFile.getAbsolutePath());
      System.out.println("Continuing...");
    }
    try {
      EngineInitializer.initializeVisualBeans();
      initializeArcGISLicenses();
      SunPosition thisApp = new SunPosition();
      thisApp.initUI(globeTextureFile);
    } catch (Exception ex) {
      System.out.println("Sample failed: " + ex.getMessage());
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      AoInitialize ao = new AoInitialize();
      
      if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
      
      ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Initialize user interface.
   */
  private void initUI(File globeTextureFile) throws AutomationException, IOException {
    //
    // set the attributes for the application's JFrame window
    //
    this.setTitle("Sun Position Sample Application");
    setSize(800, 800);
    //
    // Create the tools to be placed in the toolbar bean.
    //
    SunPositionTool sunPositionTool = new SunPositionTool();
    //
    // Create the toolbar bean for the tools and add the tools to it.
    //
    ToolbarBean toolbarBean = new ToolbarBean();
        toolbarBean.addItem(new ControlsGlobeNavigateTool(), 0, -1, false, 0, 1);
    toolbarBean.addItem(sunPositionTool, -1, -1, false, 0, esriCommandStyles.esriCommandStyleTextOnly);
    //
    // Set the layout scheme with a layout manager.
    //
    getContentPane().setLayout(new BorderLayout());
    //
    // Add the toolbar bean to the app's window frame's content pane
    //
    getContentPane().add(toolbarBean, BorderLayout.NORTH);
    //
    // Create the globe bean and associate it with the toolbar bean.
    //
    GlobeBean globeBean = new GlobeBean();
    toolbarBean.setBuddyControl(globeBean);
    //
    // Add the globe bean to the app's window frame's content pane.
    //
    getContentPane().add(globeBean, BorderLayout.CENTER);
    //
    // Make the frame visible.  You must do this before you can load a 3D/texture file
    //
    setVisible(true);
    //
    // Load the globe texture.
    //
    String globeTexture = globeTextureFile.getAbsolutePath();
    if (globeBean.check3dFile(globeTexture)) {
      System.out.println("Loading 3D file " + globeTexture + " ... ");
      try {
        globeBean.load3dFile(globeTexture); // looks like this has to be done after setVisible(true) is called
        System.out.println("File loaded.");
      }
      catch (Exception e) {
        e.printStackTrace();
        System.out.println("Continuing ...");
      }
    }
    //
    // Add a window closing operation listener to shut down arcobjects and exit the app.
    //
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          new AoInitialize().shutdown();
        } catch (Exception e1) {
          e1.printStackTrace();
        }
        System.out.println("Done.");
        System.exit(0);
      }
    });
  }
}