`
czwangelo
  • 浏览: 70829 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

sun position的engine例子

阅读更多

大家好,今天我将给大家演示engine的一个例子,

这个例子非常小,但是却也非常有趣,是关于一个地球与太阳的例子,在我们的例子中,有一个太阳,有一个地球,

我们都知道如果太阳在地球的左边,那么地球的左边是白天,而右边没被晒到的部分都是黑夜.

我们从File->New -> Others ->

选中SunPosition

点击finish,我们可以在左边的package explorer看到

同时呢,我们可以在右边看到已经打开的SunPosition的源代码,

运行该源代码,

 

在左上角我们可以看到一个小图标,点击该小图标,我们可以对地图进行转动

 

点击左上解的set sun positon

然后在地球的旁边的某个位置点击一下,就可以设制为太阳的位置了,这样地球相反的某个方向就会出现影印了,

怎么样,这个例子就是这样的,是不是觉得很有趣啊??

 

接下来,我们就对代码进行分析了

 

/*
 Copyright 1995-2006 ESRI
 
 All rights reserved under the copyright laws of the United States.
 
 You may freely redistribute and use this sample code, with or without modification.
 
 Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
 WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY 
 WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 SUCH DAMAGE.
 
 For additional information contact: Environmental Systems Research Institute, Inc.
 
 Attn: Contracts Dept.
 380 New York Street
 Redlands, California, U.S.A. 92373 
 Email: contracts@esri.com
 */
package arcgissamples.globecore.sunposition;


import java.awt.*;
import javax.swing.*;

import com.esri.arcgis.beans.globe.GlobeBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.controls.ControlsGlobeNavigateTool;
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.systemUI.esriCommandStyles;
import com.esri.arcgis.interop.AutomationException;
//import com.esri.arcgis.interop.AutomationException;

import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class SunPosition extends JFrame {
	public static void main(String[] args) {
		System.out.println("Starting Sun Position - An ArcObjects Java SDK Developer Sample");
		String arcGISHome = null;
		arcGISHome = System.getenv("ARCGISHOME");
		if (arcGISHome == null || !(new File(arcGISHome).exists())) {
			System.out.println(((arcGISHome==null)?"ARCGISHOME environment variable":arcGISHome)+" 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 = arcGISHome + "java/samples/data/globe_data/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();
			AoInitialize aoInitializer = new AoInitialize();
			aoInitializer.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
			aoInitializer.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
			SunPosition thisApp = new SunPosition();
			thisApp.initUI(globeTextureFile);
		} catch (Exception ex) {
			System.out.println("Sample failed: " + ex.getMessage());
			ex.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);
			}
		});
	}
}

 

 

 

 

 

/*
 Copyright 1995-2006 ESRI
 
 All rights reserved under the copyright laws of the United States.
 
 You may freely redistribute and use this sample code, with or without modification.
 
 Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
 WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY 
 WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 SUCH DAMAGE.
 
 For additional information contact: Environmental Systems Research Institute, Inc.
 
 Attn: Contracts Dept.
 380 New York Street
 Redlands, California, U.S.A. 92373 
 Email: contracts@esri.com
 */
package arcgissamples.globecore.sunposition;

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 {
	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 abd 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};
			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;
	}		
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics