ArcGisEngineForJava开发

ArcGIS Engine control examples

一、利用Visual JavaBeans来构建应用程序

这种方案是针对使用可视化的Java组件,想要来构建和部署应用程序的开发人员。Java平台的ArcGIS引擎软件开发工具包(SDK)中概述了使用Visual Java Beans来构建和部署应用程序的过程。在相同的安装程序位置中能够找到已完成的应用程序。作为ArcGis开发样品的一部分,安装了ArcGis开发示例。

在窗口平台上,该示例是在以下位置:

%arcgishome%sample\Java\Engine\scenario.mapviewer

在Solaris和Linux平台,示例在以下位置:

% arcgishome %/Java/sample/engine/ scenario.mapviewer

二、项目描述

这个方案演示所需的步骤来创建一个用于查看预先授过权的ESRI地图文件的应用(mxds)。该方案涵盖以下领域:

  • 建立开发环境
  • 使用可视化组件构建图形用户界面(GUI)
  • 载入地图文件
  • 向工具栏添加命令
  • 关联ToolbarBean和TOCBean
  • 添加工具栏选项到ToolbarBean中
  • 使用Toolbar Menu来创建一个弹出式菜单
  • 在TOCBean组件中管理、编辑标签
  • 在MapBean绘制矩形概述
  • 创建自定义工具
  • 自定义ToolbarBean
  • 使用一个可执行的Java档案(JAR)文件来部署

    三、概念

    Java平台的ArcGIS Engine SDK提供了可重用的可视化Java组件。这种开发方案显示了如何将这些组件嵌入到一个Java用户界面(用户界面)来构建地图浏览器应用程序。在JavaBeans兼容的集成开发环境(IDE)中,这种Visual Beans是遵循Visual JavaBeans组件架构的,并且允许它们在设计Java图形用户界面时作为拖放组件使用。每个组件具有特定的属性和方法,并且能够触发事件。在一个Java应用程序中,在支持ArcGIS Engine平台上,通过ArcGIS Engine可视组件彼此之间,与非可视ArcObjects组件之间的装配、布线,可以快速建立和部署自定义地理信息系统(GIS)的应用。

    四、设计

    在这个应用程序中,该MapBean,PagelayoutBean,TocBean,和ToolbarBean组件被放在一个javax.swing.jframe容器中,并彼此之间、与其他ArcGIS Engine对象之间互相交流,以提供GIS查看功能。MapBean服务作为应用程序一角的概览图,而PagelayoutBean则是整体视图中心。

    该方案首先使用BorderLayout管理器固定组件位置,进而构建一个GUI。一旦组件被添加到JFrame容器,使用setbuddy()方法让它们相互连接。在这个阶段中,应用程序可以作为一个简单的地图浏览器。

    这个方案之后扩展那个简单地图浏览器的功能,通过建立自定义的工具和演示事件处理。为此,探讨了应用程序编程接口(API)的视觉和非视觉的ArcGIS Engine组件。

    一个支持可视化GUI设计的Java IDE中,组件可作为拖放JavaBeans组件来使用,在这种情况下,组件将以编程方式被放置来获得更容易理解的代码。

    五、要求

    要成功地遵循这种方案,您需要以下(对于有关部署的要求的信息,见此主题中的部署节):

  • 一个有能用来开发使用的授权文件的Java平台ArcGIS Engine SDK的安装;
  • 一个安装的J2SE JDK开发包,1.5或更高版本的。
  • 支持的Java IDE或您最喜爱的文本编辑器。
  • 一个初级到中级的编程语言的知识。
  • 而与其他ESRI软件不需要经验相比,有基于ArcObjects和地图的基本知识、经历的经验是最好的。

    访问下面的示例数据和解决方案代码:

    %ARCGISHOME%\sample\Java\engine\developer_guide_scenarios.arcgis_engine.building_an_arcgis_control_application.mapviewer

    为了构建那个应用程序,下面ArcGIS Engine中的Visual JavaBeans将被使用:

    com.esri.arcgis.beans.map.mapbean

    com.esri.arcgis.beans.pagelayout.pagelayoutbean

    com.esri.arcgis.beans.toc.tocbean

    com.esri.arcgis.beans.toolbar.toolbarbean

    com.esri.arcgis.controls.toolbarmenu

    此外,下面的包中的对象将被使用:

    com.esri.arcgis.carto

    com.esri.arcgis.controls

    com.esri.arcgis.display

    com.esri.arcgis.geometry

    com.esri.arcgis.system

    com.esri.arcgis.systemUI

    为了参考以前的包,arcobjects.jar必须被添加到您的类路径中。那个arcobjects.jar文件位于%arcgishome%\ArcGIS\Java\lib中。

    六、实施

    为了执行该场景,按照本节的步骤。虽然该方案具体地使用了海湾地区的圣劳伦斯地图文件安装的样品,你可以使用你的地图文件。实施为您提供所有需要成功完成该场景的代码。它不会提供一步一步的指令来开发应用程序在爪哇,因为它假定你已经有一个工作知识的发展环境。

    七、建立发展环境

    编译并运行应用程序,使用ArcGIS Engine,设置您的开发环境。了解更多信息,查看安装后。

    八、使用可视化组件构建图形用户界面

    做以下步骤:

    1、创建一个mapviewerframe.java文件。那个MapViewerFrame类提供了地图查看器应用程序的界面和功能。实现这个类,并作为javax.swing.jframe的子类。见下面的代码示例:

    [Java]

    // MapViewerFrame.java.

    import java.io.IOException;

    import javax.swing.JFrame;

    public class MapViewerFrame extends JFrame{

    // Constructor.

    public MapViewerFrame(){

    setTitle("MapViewer");

    }

    public void buildAndShow()throws IOException{

    this.setVisible(true);

    }

    } // End MapViewerFrame class.

    2创建一个MapViewer.java文件。本文件提供的主要方法,构建MapViewer窗体,给它一个初始的大小,并运行那个窗体。见下面的代码示例:

    [Java]

    // MapViewer.java.

    import java.awt.Dimension;

    import java.awt.Toolkit;

    import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

    import java.io.IOException;

    import javax.swing.UIManager;

    import com.esri.arcgis.system.AoInitialize;

    import com.esri.arcgis.system.EngineInitializer;

    import com.esri.arcgis.system.esriLicenseProductCode;

    public class MapViewer{

    public static void main(String[] args)throws IOException{

    EngineInitializer.initializeVisualBeans();

    String laf = UIManager.getSystemLookAndFeelClassName();

    UIManager.setLookAndFeel(laf);

    final AoInitialize aoInit = new AoInitialize();

    aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

    MapViewerFrame mapViewerFrame = new MapViewerFrame();

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    int x = d.width / 6;

    int y = d.height / 6;

    int width = d.width * 2 / 3;

    int height = d.height * 2 / 3;

    mapViewerFrame.setBounds(x, y, width, height);

    mapViewerFrame.addWindowListener(new WindowAdapter(){

    public void windowClosing(WindowEvent e){

    try{

    aoInit.shutdown();

    }

    catch (IOException ex){

    ex.printStackTrace();

    }

    System.exit(0);

    }

    }

    );

    mapViewerFrame.buildAndShow();

    }

    } // End MapViewer class.

    3。编译MapViewerFrame和MapViewer的Java文件。要做到这一点,Java编译器需要知道在哪里找到引用的Java类。这是在包含arcobjects.jar的类路径中来做的。为了使用命令行编译,使用cd命令来改变含MapViewerFrame和MapViewer的Java代码的目录:

    javac - classpath % ARCGISHOME % \java\lib\arcobjects.jar;. * .java

    前面的代码示例编译Java程序并生成应用程序的类文件。如果你得到NoClassDefFoundError,检查你的classpath和验证在指定的位置存在arcobjects.jar。

    4。运行MapViewer类,并验证一个空白的Java窗体出现后再进行下一步。为了运行Java程序,单击"运行"按钮在您的IDE或在命令行中输入以下命令:

    java - classpath % ARCGISHOME %\java\lib\arcobjects.jar;. MapViewer

    5。MapViewerFrame类中添加为这些组件添加成员变量,并在构造函数中创建这些组件。见下面的代码示例:

    [Java]

    // MapViewerFrame.java.

    import java.io.IOException;

    import javax.swing.JFrame;

     

    // Add new imports for this step.

    import javax.swing.JLabel;

    import java.awt.BorderLayout;

    import java.awt.Dimension;

    import javax.swing.JPanel;

    import com.esri.arcgis.beans.TOC.TOCBean;

    import com.esri.arcgis.beans.map.MapBean;

    import com.esri.arcgis.beans.pagelayout.PageLayoutBean;

    import com.esri.arcgis.beans.toolbar.ToolbarBean;

     

    public class MapViewerFrame extends JFrame{

    PageLayoutBean pageLayout;

    MapBean map;

    TOCBean toc;

    ToolbarBean toolbar;

    JLabel statusLabel;

    // Constructor.

    public MapViewerFrame(){

    setTitle("MapViewer");

    pageLayout = new PageLayoutBean();

    map = new MapBean();

    toc = new TOCBean();

    toolbar = new ToolbarBean();

    statusLabel = new JLabel(" ");

    }

    }

    6。在MapViewerFrame界面,toolbar组件占据了BorderLayout的NORTH位置,PageLayout组件占据了BorderLayout的CENTER位置。(TOC)目录和map组件在一个Jpanel中,它坐落在BorderLayout的WEST位置。为了实现这一目标,创造一个方法来建立一个包含恰当布局的两个控件的Jpanel。见下面的代码示例:

    [Java]

    // New method to build the left panel.

    private JPanel buildMapTOCPanel(){

    JPanel leftPanel = new JPanel();

    JPanel bottomPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());

    bottomPanel.setLayout(new BorderLayout());

    bottomPanel.setPreferredSize(new Dimension(200, 200));

    bottomPanel.add(map, BorderLayout.CENTER);

    leftPanel.add(toc, BorderLayout.CENTER);

    leftPanel.add(bottomPanel, BorderLayout.SOUTH);

    return leftPanel;

    }

    7。在MapViewerFrameBorderLayout的SOUTH位置,添加JLabel作为一个状态栏。那个buildAndShow()方法看起来像下面的代码示例当你更新了所有的布置位置:

    [Java]

    public void buildAndShow()throws IOException{

    JPanel mapTOCPanel = buildMapTOCPanel();

    this.getContentPane().add(toolbar, BorderLayout.NORTH);

    this.getContentPane().add(pageLayout, BorderLayout.CENTER);

    this.getContentPane().add(mapTOCPanel, BorderLayout.WEST);

    this.getContentPane().add(statusLabel, BorderLayout.SOUTH);

     

    this.setVisible(true);

    }

    8、编译和运行应用程序以确认图形用户界面的布局类似于下面的屏幕截图:

    一个标准的Java布局管理器(BorderLayout)已被用来定位组件和管理组件的布局和尺寸。你将能够调整窗口,并适当地检查更新该组件而不需要任何明确的调整代码。

    九、载入地图文件

    做以下步骤:

    1、当增加组件的时候,为控件加载地图文件,通过添加下面的引入到MapViewer.java文件中:

    [Java]

    import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter;

    import com.esri.arcgis.controls.IPageLayoutControlEventsOnPageLayoutReplacedEvent;

    2、添加下面的代码示例到buildAndShow方法调用之后,在调用this.setVisible()方法之前:

    为了跨平台的兼容性,验证用于存储数据的文件和路径名必须是小写文本。

    [Java]

    public void buildAndShow()throws IOException{

    . . . . . . addEventListeners();

    // Load a preauthored map document into the PageLayout bean.

     

    String arcgisHome = System.getenv("ARCGISHOME");

    String documentPath = arcgisHome +

    "java/samples/data/mxds/gulf of st. lawrence.mxd";

    if (pageLayout.checkMxFile(documentPath)){

    pageLayout.loadMxFile(documentPath, null);

    }

    this.setVisible(true);

    3、当MXD文档被加载到PageLayoutBean中,会产生一个onPageLayoutReplaced事件。在响应于此事件的地图组件中添加代码以加载相同的地图文档。在addEventListeners()方法中,使用IPageLayoutControlEventsAdapter作为一个匿名内部类为PageLayout组件的onPageLayoutReplaced事件添加事件监听器。见下面的代码示例:

    [Java]

    private void addEventListeners()throws IOException{

    pageLayout.addIPageLayoutControlEventsListener(new

    IPageLayoutControlEventsAdapter(){

    public void onPageLayoutReplaced

    (IPageLayoutControlEventsOnPageLayoutReplacedEvent evt)throws

    IOException{

    map.loadMxFile(pageLayout.getDocumentFilename(), null, null);

    map.setExtent(map.getFullExtent());

    }

    }

    );

    }

    十、建立互联的ToolbarBean和TOCBean

    组件互相之间不知道对方,即使他们已经加入到JFrame。为了控制他们之间的同步工作,TOC和toolbar组件应该知道与他们相关的控制。否则,toolbar组件将不知道哪个组件在控制,并且TOC组件将不知道它应该显示哪个组件的TOC。

    为了建立组件间的通信,将下面的代码添加到buildAndShow ()方法中的加载文件的代码之后:

    [Java]

    public void buildAndShow()throws IOException{

    . . .

    // Load a preauthored map document on the pageLayout component.

     

    String arcgisHome = System.getenv("ARCGISHOME");

    String documentPath = arcgisHome +

    "java/samples/data/mxds/gulf of st. lawrence.mxd";

    if (pageLayout.checkMxFile(documentPath))

    pageLayout.loadMxFile(documentPath, null);

    // Set buddy controls to wire up the TOC and toolbar beans

    // with the PageLayout bean.

    toc.setBuddyControl(pageLayout);

    toolbar.setBuddyControl(pageLayout);

    }

     

    this.setVisible(true);

    十一、向toolbar(工具栏)添加命令

    工具栏控件已添加到用户界面中。默认情况下,该控件没有填充任何工具。您将在下面的步骤中添加工具。

    ArcGIS Engine集合了120多个与mapbean,pagelayoutbean,和toolbarbean运作的命令和工具。这些命令和工具提供了大量的经常使用的地理信息系统地图导航,图形管理,特征选择的功能。您将添加一些这些命令和工具到您的应用程序。

    1、为了添加这些预建的工具栏命令,添加下面的引入到MapViewerFrame.java中:

    [Java]

    import com.esri.arcgis.controls.ControlsMapFullExtentCommand;

    import com.esri.arcgis.controls.ControlsMapPanTool;

    import com.esri.arcgis.controls.ControlsMapZoomInTool;

    import com.esri.arcgis.controls.ControlsMapZoomOutTool;

    import com.esri.arcgis.controls.ControlsOpenDocCommand;

    import com.esri.arcgis.controls.ControlsPagePanTool;

    import com.esri.arcgis.controls.ControlsPageZoomInTool;

    import com.esri.arcgis.controls.ControlsPageZoomOutTool;

    import com.esri.arcgis.controls.ControlsPageZoomPageToLastExtentBackCommand;

    import com.esri.arcgis.controls.ControlsPageZoomPageToLastExtentForwardCommand;

    import com.esri.arcgis.controls.ControlsPageZoomWholePageCommand;

    import com.esri.arcgis.systemUI.esriCommandStyles;

    2、在buildAndShow()方法中,在调用addEventListeners()方法之前,给工具栏添加预定义的命令。见下面的代码示例:

    [Java]

    // Add generic commands to the toolbar.

    toolbar.addItem(new ControlsOpenDocCommand(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPageZoomInTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPageZoomOutTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPagePanTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPageZoomWholePageCommand(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPageZoomPageToLastExtentBackCommand(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsPageZoomPageToLastExtentForwardCommand(), 0, - 1, false,

    0, esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsMapZoomInTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsMapZoomOutTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsMapPanTool(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new ControlsMapFullExtentCommand(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    addEventListeners();

    // Load a preauthored map document on the pageLayout component.

    3、运行应用程序。地图文件被加载到PageLayout组件中,并且TOC组件列出了如在下面的屏幕截图所显示得数据图层。用TOC组件切换图层的可见。默认情况下,地图文档的焦点地图被加载到地图组件中,并且命令已经被添加到工具栏组件中。

    十二、创建一个使用ToolbarMenu弹出菜单

    ArcGIS Engine提供了一个的ToolbarMenu组件,它可以用来添加弹出式菜单到其他组件中去,诸如MapBean和PageLayoutBean。在本节中,您将添加一个弹出式菜单到页面布局中。弹出式菜单包含了从com.esri.arcgis.controls包中的预建立好的命令。

    为了展示弹出式菜单,一个onMouseDown的事件处理程序将被添加为PageLayout组件中。右键单击时,该菜单出现。

    1、添加下面的引入到mapviewerframe.java文件中:

    [Java]

    import com.esri.arcgis.controls.ToolbarMenu;

    import com.esri.arcgis.controls.IPageLayoutControlEventsOnMouseDownEvent;

    import com.esri.arcgis.controls.ControlsPageZoomInFixedCommand;

    import com.esri.arcgis.controls.ControlsPageZoomOutFixedCommand;

    2、为ToolbarMenu添加一个成员变量。见下面的代码示例:

    [Java]

    public class MapViewerFrame extends JFrame{

    PageLayoutBean pageLayout;

    MapBean map;

    TOCBean toc;

    ToolbarBean toolbar;

    JLabel statusLabel;

    ToolbarMenu popupMenu;

    3、在一个try/catch语句模块中构造popupMenu对象。见下面的代码示例:

    [Java]

    // Constructor.

    public MapViewerFrame(){

    setTitle("MapViewer");

    pageLayout = new PageLayoutBean();

    map = new MapBean();

    toc = new TOCBean();

    toolbar = new ToolbarBean();

    statusLabel = new JLabel(" ");

    try{

    popupMenu = new ToolbarMenu();

    }

    catch (IOException e){

    e.printStackTrace();

    }

    }

    4、在buildAndShow()方法里面,在往工具栏中添加命令之前,从com.esri.arcgis.controls包中添加预先建立好的命令到popupMenu组件中。见下面的代码示例:

    [Java]

    // Add popup menu items.

    popupMenu.addItem(new ControlsPageZoomInFixedCommand(), 0, - 1, false,

    esriCommandStyles.esriCommandStyleIconAndText);

    popupMenu.addItem(new ControlsPageZoomOutFixedCommand(), 0, - 1, false,

    esriCommandStyles.esriCommandStyleIconAndText);

    popupMenu.addItem(new ControlsPageZoomWholePageCommand(), 0, - 1, false,

    esriCommandStyles.esriCommandStyleIconAndText);

    popupMenu.addItem(new ControlsPageZoomPageToLastExtentBackCommand(), 0, - 1, false,

    esriCommandStyles.esriCommandStyleIconAndText);

    popupMenu.addItem(new ControlsPageZoomPageToLastExtentForwardCommand(), 0, - 1,

    false, esriCommandStyles.esriCommandStyleIconAndText);

    // Add generic commands to the toolbar.

    . . .

    5、在buildAndShow()方法中,使用建立组件之间互相联系的代码来关联popupMenu和PageLayout组件。联想与代码页设置的好友控制在。见下面的代码示例:

    [Java]

    // Set buddy controls to wire up the TOC and ToolbarBean

    // with the pageLayout object.

    toc.setBuddyControl(pageLayout);

    toolbar.setBuddyControl(pageLayout);

    popupMenu.setHook(pageLayout);

    }

     

    this.setVisible(true);

    6、在addEventListeners ()方法中,使用IpageLayoutControlEventsAdapter,作为一个匿名的内部类,为onMouseDown事件添加一个事件监听器到pageLayout组件中去。见下面的代码示例:

    [Java]

    pageLayout.addIPageLayoutControlEventsListener(new IPageLayoutControlEventsAdapter(){

    public void onPageLayoutReplaced

    (IPageLayoutControlEventsOnPageLayoutReplacedEvent evt)throws IOException{

    map.loadMxFile(pageLayout.getDocumentFilename(), null, null); map.setExtent

    (map.getFullExtent());

    }

    public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent evt)throws

    IOException{

    // If the right mouse button is clicked, display the popup menu.

    if (evt.getButton() == 2){

    popupMenu.popupMenu(evt.getX(), evt.getY(), pageLayout.getHWnd());

    }

    }

    }) ;

    7、运行应用程序。右键单击pageLayout组件来显示弹出菜单,并且可在pageLayout内导航。看下面的屏幕截图:

    十三、在TOCBean中管理标签编辑

    默认情况下,该TOCBean允许用户自动切换图层的可见性,并改变地图和图层名称当他们出现在TOC中时。您将添加代码,以防止用户编辑一个名称,并用一个空字符串替换它。

    1、为了类在这一步中可以使用,添加下面的引用到mapviewerframe.java文件中:

    [Java]

    import com.esri.arcgis.controls.ITOCControlEventsAdapter;

    import com.esri.arcgis.controls.ITOCControlEventsOnEndLabelEditEvent;

    import com.esri.arcgis.controls.esriTOCControlEdit;

    2、在buildAndShow()方法中,设置labeledit手册。见下面的代码示例

    [Java]

    // Set label editing to manual.

    toc.setLabelEdit(esriTOCControlEdit.esriTOCControlManual);

    // Add pop-up menu items.

    . . .

    3、在addEventListeners ()方法中,使用ITOCControlEventsAdapter,作为一个匿名的内部类,为onEndLabelEdit事件添加一个事件监听器到TOC组件中去。见下面的代码示例:

    [Java]

    private void addEventListeners()throws IOException{

    . . . toc.addITOCControlEventsListener(new ITOCControlEventsAdapter(){

    public void onEndLabelEdit(ITOCControlEventsOnEndLabelEditEvent labelEditEvt)

    throws IOException{

    String newLabel = labelEditEvt.getNewLabel();

    // If the new label is an empty string, prevent the edit.

    if (newLabel.equals("")){

    labelEditEvt.setCanEdit(false);

    }

    }

    });

    }

    4、运行应用程序。在TOC组件中,为了编辑地图,点击图层,标题,或图例类label,然后再次单击来调用label编辑。用空字符串替换标签。你可以除空字符串之外的字符串来替换那个label。在编辑的任何时候按ESC都可取消,。看到下面的屏幕截图:

    十四、在MapBean上绘制矩形概述

    其次,使用map组件作为一个总览窗口,在pageLayout组件中,在展示焦点的地图的范围上面绘制。为了浏览map组件的总览窗口更新,在PageLayout组件的数据窗体中导航数据。中浏览在版面组成数据帧数据看地图组件的窗口更新。

    1、为了类在这一步使用,添加以下引入到mapviewerframe.java文件中:

    [Java]

    import com.esri.arcgis.controls.IMapControlEvents2Adapter;

    import com.esri.arcgis.controls.IMapControlEvents2OnAfterDrawEvent;

    import com.esri.arcgis.controls.IPageLayoutControlEventsOnPageLayoutReplacedEvent;

    import com.esri.arcgis.carto.Map;

    import com.esri.arcgis.carto.esriViewDrawPhase;

    import com.esri.arcgis.display.DisplayTransformation;

    import com.esri.arcgis.display.ITransformEventsAdapter;

    import com.esri.arcgis.display.ITransformEventsVisibleBoundsUpdatedEvent;

    import com.esri.arcgis.display.RgbColor;

    import com.esri.arcgis.display.SimpleFillSymbol;

    import com.esri.arcgis.display.SimpleLineSymbol;

    import com.esri.arcgis.geometry.IEnvelope;

    2、添加下面的类成员到mapViewerFrame中:

    [Java]

    public class MapViewerFrame extends JFrame{

    SimpleFillSymbol fillSymbol; // The symbol used to draw the envelope.

    IEnvelope currentExtent; // The envelope drawn on the MapBean.

    Map focusMap; // The PageLayoutBean's focus map.

    PageLayoutBean pageLayout;

    . . .

    3、创建一个createOverviewSymbol ()的私有方法。在pageLayoutBean中,你将在map控件中创建符号用来代表数据的范围。见下面的代码示例:

    [Java]

    private void createOverviewSymbol()throws IOException{

    RgbColor color = new RgbColor();

    color.setRed(255);

    color.setGreen(0);

    color.setBlue(0);

    color.setTransparency((byte)255);

    SimpleLineSymbol outline = new SimpleLineSymbol();

    outline.setColor(color);

    fillSymbol = new SimpleFillSymbol();

    color.setTransparency((byte)0);

    fillSymbol.setColor(color);

    fillSymbol.setOutline(outline);

    }

    4、在从buildAndShow()方法中调用createOverviewSymbol方法之后,设置label编辑手册。见下面的代码示例:

    [Java]

    // Set label editing to manual.

    toc.setLabelEdit(esriTOCControlEdit.esriTOCControlManual);

    // Create symbol used to draw overview on the MapBean.

    createOverviewSymbol();

    // Add pop-up menu items.

    . . .

    5、在IpageLayoutControlEvents的事件监听器(以前添加到addEventListeners ()方法中)里,得到一个PageLayout组件焦点图的引用。在currentExtent成员变量中存储focusMap的范围。这个范围将用来绘制地图组件的矩形概述。为了实现这一目标,在加载地图文件之后添加下面的示例代码,在onPageLayoutReplaced事件处理程序中设置它们的范围:

    [Java]

    pageLayout.addIPageLayoutControlEventsListener(new IPageLayoutControlEventsAdapter(){

    public void onPageLayoutReplaced

    (IPageLayoutControlEventsOnPageLayoutReplacedEvent evt)throws IOException{

    map.loadMxFile(pageLayout.getDocumentFilename(), null, null); map.setExtent

    (map.getFullExtent()); focusMap = new Map(pageLayout.getActiveView()

    .getFocusMap()); currentExtent = focusMap.getExtent();

    }

    }

     

    );

    6、该PageLayoutBean的默认事件是IPageLayoutControlEvents。这些事件不是告诉你何时数据窗体内的地图范围的发生变化。为了做这个,以PageLayoutBean焦点图的ItransformEvents为陷阱。添加下面的事件监听器来监听DisplayTransformation对象的visibleBoundsUpdated事件。

    当map的范围被改变时,该visibleBoundsUpdated事件被触发,并被用来设置Ienvelope作为一个新的可视的地图范围。通过刷新MapBean,你迫使它重新绘制它展示出来的形状。修改在前一步中编辑的监听器。见下面的代码示例:

    [Java]

    pageLayout.addIPageLayoutControlEventsListener(new IPageLayoutControlEventsAdapter(){

    public void onPageLayoutReplaced

    (IPageLayoutControlEventsOnPageLayoutReplacedEvent evt)throws IOException{

    map.loadMxFile(pageLayout.getDocumentFilename(), null, null); map.setExtent

    (map.getFullExtent()); focusMap = new Map(pageLayout.getActiveView()

    .getFocusMap()); currentExtent = focusMap.getExtent();

    DisplayTransformation dt = new DisplayTransformation

    (focusMap.getScreenDisplay().getDisplayTransformation());

    dt.addITransformEventsListener(new ITransformEventsAdapter(){

    public void visibleBoundsUpdated

    (ITransformEventsVisibleBoundsUpdatedEvent evt)throws IOException{

    // Set currentExtent to the new visible extent.

    currentExtent = evt.getSender().getVisibleBounds();

    // Refresh the map components foreground phase.

    map.refresh(esriViewDrawPhase.esriViewForeground, null, null);

    }

    }

    );

    }

    public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent evt)throws

    IOException{

    7、当一个map的由visibleBoundsUpdated事件处理程序的刷新操作被触发时,添加IMapControlEvents2Listener到绘制那个更新后的范围边界的map组件中,并加入到前面的步骤中。将下面的示例代码添加到addEventListeners ()方法中:

    [Java]

    map.addIMapControlEvents2Listener(new IMapControlEvents2Adapter(){

    public void onAfterDraw(IMapControlEvents2OnAfterDrawEvent evt)throws

    IOException{

    if (evt.getViewDrawPhase() == esriViewDrawPhase.esriViewForeground){

    try{

    // Draw the shape on the MapBean.

    map.drawShape(currentExtent, fillSymbol);

    }

    catch (Exception e){

    System.err.println("Error in drawing shape on MapBean");

    // e.printStackTrace();

    }

    }

    }

    });

    8、运行应用程序。在pageLayout中,使用前面添加的地图导航工具来改变焦点地图的范围,并且观察在map组件上面,作为一个红的矩形,被画出的新的范围。看到下面的屏幕截图:

    在PageLayoutBean中,使用地图导航工具来导航那个焦点地图,改变了焦点地图的范围,并且引起MapBean随之更新。使用页面的导航工具来导航页面布局,改不了页面布局的范围(不是在PageLayoutBean中的焦点地图的范围),并没有致使MapBean发生更新。

     

    十五、创建自定义工具

    做以下步骤:

    创建一个自定义的工具添加到ToolbarBean中。这个工具在鼠标单击的位置上添加今天的日期作为一个文本元素到PageLayoutBean中。该工具将被建立作为一个通用的工具,是为了它可以与MapBean、ToolbarBean以及PageLayoutBean工作。自定义工具可以被建立作为Java类扩展com.esri.arcgis.controls.BaseTool。

    该BaseTool类使得它更容易创建自定义工具而不是直接实现com.esri.arcgis.systemUI.ICommand和com.esri.arcgis.systemUI.ITool。通过扩展基类,你可以避免为Icommand和Itool接口写空的实现方法。

    1、创建一个AddDateTool.java文件。在默认的构造函数中,设置一些BaseTool的受保护的(protected)变量的值。见下面的代码示例:

    [Java]

    // AddDateTool.java.

    import com.esri.arcgis.controls.BaseTool;

    public class AddDateTool extends BaseTool{

    public AddDateTool(){

    //Set various protected properties specified by BaseTool.

    name = "CustomCommands_Add Date";

    caption = "Add date";

    toolTip = "Add date";

    message = "Adds a date element to the page layout";

    category = "CustomCommands";

    enabled = true;

    }

    } // End AddDateTool class.

    2、该onCreate方法是通过引用来控制命令工作。在这种情况下,它可以是一个MapBean,PageLayoutBean,或ToolbarBean。而不是在onCreate方法中添加逻辑来决定对象传递给钩子方法的类型,使用HookHelper类来处理这个。一个命令或工具需要知道如何处理它传递的钩子,所以必需要检查来确定已经通过的ArcGIS控件的类型。

    该HookHelper是用来保留钩子,并且返回ActiveView任何钩型(在这种情况下,一个MapBean,PageLayoutBean,或ToolbarBean)。引入HookHelper类,并添加HookHelper成员变量到AddDateTool类中。见下面的代码示例:

    [Java]

    import com.esri.arcgis.controls.HookHelper;

    public class AddDateTool extends BaseTool{

    HookHelper hookHelper;

    . . .

    3、重写BaseTool.onCreate方法。在这里,构建HookHelper,并使用setHookByRef方法来传递控件对象。BaseTool.onCreate方法不被声明为抛出任何异常,但HookHelper可以抛出IOException或AutomationException。因此,在你的onCreate方法中,代码必须捕获(trap/catch)并处理这些异常。这个简单的例子处理了一个被封装在一个RuntimeException中异常,并抛出它到栈处理上面。见下面的代码示例:

    [Java]

    public void onCreate(Object obj){

    try{

    hookHelper = new HookHelper();

    hookHelper.setHookByRef(obj);

    }

    catch (IOException ex){

    System.out.println(

    "Exception caught inside AddDateTool.onCreate(). Throwing as new RuntimeException.");

    throw new RuntimeException(ex);

    }

    }

    4、添加类和将由onMouseDown方法使用的接口的导入语句。见下面的代码示例:

    [Java]

    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import com.esri.arcgis.carto.IActiveView;

    import com.esri.arcgis.carto.TextElement;

    import com.esri.arcgis.carto.esriViewDrawPhase;

    import com.esri.arcgis.display.TextSymbol;

    import com.esri.arcgis.geometry.IPoint;

    import com.esri.arcgis.support.ms.stdole.StdFont;

    5、重写onMouseDown方法,它创建了一个使用当前日期作为文本的TextElement,并将其添加到挂钩对象的图形容器中。该AddDateTool类是完整的,且是可编译的。见下面的代码示例:

    [Java]

    public void onMouseDown(int button, int shift, int x, int y){

    try{

    Date today = new Date();

    // Format the date in the form of "Wed 25 Aug, 2004".

    SimpleDateFormat formatter;

    formatter = new SimpleDateFormat("EEE d MMM, yyyy");

    String dateString = formatter.format(today);

    // Create a font.

    StdFont font = new StdFont();

    font.setName("Arial");

    font.setBold(true);

    // Create a text symbol.

    TextSymbol textSymbol = new TextSymbol();

    textSymbol.setFont(font);

    textSymbol.setSize(15);

    // Create a text element to add to the graphics container.

    TextElement textElement = new TextElement();

    textElement.setSymbol(textSymbol);

    textElement.setScaleText(false);

    textElement.setText(dateString);

    // Add the text element to the graphics container.

    IActiveView activeView = hookHelper.getActiveView();

    IPoint pt = activeView.getScreenDisplay().getDisplayTransformation()

    .toMapPoint(x, y);

    textElement.setGeometry(pt);

    activeView.getGraphicsContainer().addElement(textElement, 0);

    // Refresh the view.

    activeView.partialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

    }

    catch (IOException ex){

    System.out.println(

    "Exception caught inside AddDateTool.onCreate(). Throwing as new RuntimeException.");

    throw new RuntimeException(ex);

    }

    }

    6、在MapViewerFrame中,添加AddDateTool的实例到toolbar组件的buildAndShow ()方法中的添加预定义命令到工具栏的代码行之后。见下面的代码示例:

    [Java]

    toolbar.addItem(new ControlsMapFullExtentCommand(), 0, - 1, false, 0,

    esriCommandStyles.esriCommandStyleIconOnly);

    toolbar.addItem(new AddDateTool(), 0, - 1, true, 0,

    esriCommandStyles.esriCommandStyleTextOnly);

    7、重新编译并启动MapViewer类。工具栏上出现了一个新的添加日期工具(Add Date tool)。单击该工具,然后单击"页面布局"以增加今天的日期(在那个位置)。看到下面的屏幕截图:

     

    十六、自定义ToolbarBean

    在设计时,为了添加ArcGIS Engine的命令和工具到ToolbarBean中,如同前面的代码示例所示那样使用addItem()方法,在运行时,你可以使用自定义的对话框,也可以通过自定义的ToolbarBean来添加他们。为了使这个能用,将toolbar组件放置在自定义模式中并显示"自定义"对话框。

    1、添加下面的引用到MapViewerFrame.java文件中。见下面的代码示例:

    [Java]

    import javax.swing.JCheckBox;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import com.esri.arcgis.controls.CustomizeDialog;

    import com.esri.arcgis.controls.ICustomizeDialogEvents;

    import com.esri.arcgis.controls.ICustomizeDialogEventsOnCloseDialogEvent;

    import com.esri.arcgis.controls.ICustomizeDialogEventsOnStartDialogEvent;

    作为组件对象模型(组件)组件,只有在系统中注册的工具和命令,才可以使用自定义对话框被添加到toolbar(工具栏)。Java的命令和工具没有出现在"自定义"对话框中(在前面的步骤中查看如何建立一个"自定义"对话框),因为它们没有在系统注册表中注册为COM组件。

    2、添加类成员。见下面的代码示例:

    [Java]

    . . . public class MapViewerFrame extends JFrame{

    JCheckBox customizeCB; // JCheckbox to control toolbar customization.

    CustomizeDialog customizeDialog;

    // The Customize dialog box used by the ToolbarBean constructor.

    3、创建一个createCustomizeDialog ()方法,是为了实例化那个自定义对话框。见下面的代码示例:

    [Java]

    private void createCustomizeDialog()throws IOException{

    customizeDialog = new CustomizeDialog();

    customizeDialog.setDialogTitle("Customize Toolbar Items");

    customizeDialog.setShowAddFromFile(true);

    // Set the toolbar in which to add new items.

    customizeDialog.setDoubleClickDestination(toolbar);

    }

    4、在buildandShow ()方法中,调用createCustomizeDialog ()方法来实例化自定义对话框。也实例化JcheckBox的实例名为customizeCB。见下面的代码示例:

    [Java]

    public void buildAndShow()throws IOException{

    JPanel mapTOCPanel = buildMapTOCPanel();

    createCustomizeDialog();

    customizeCB = new JCheckBox("Customize");

    5、在addEventListeners方法中,添加一个实例化JcheckBox的实例名为customizeCB的监听器来打开和关闭"自定义"对话框。见下面的代码示例:

    [Java]

    private void addEventListeners()throws IOException{

    ... customizeCB.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){

    try{

    if (customizeCB.isSelected()){

    customizeDialog.startDialog(toolbar.getHWnd());

    }

    else{

    customizeDialog.closeDialog();

    }

    }

    catch (Exception ee){

    ee.printStackTrace();

    }

    }

    }

    );

    ...

    6、在buildAndShow方法中,通过创建一个JPanel,添加自定义复选框到GUI中。使用新的复选框,移动toolbar到JPanel中。,把JPanel放置到应用程序中的BorderLayout. NORTH位置,这是toolbar使用的位置。见下面的代码示例:

    [Java]

    JPanel topPanel = new JPanel();

    topPanel.setLayout(new BorderLayout());

    topPanel.add(toolbar, BorderLayout.CENTER);

    topPanel.add(customizeCB, BorderLayout.EAST);

     

    this.getContentPane().add(topPanel, BorderLayout.NORTH);

    this.getContentPane().add(toolbar, BorderLayout.NORTH);

    this.getContentPane().add(pageLayout, BorderLayout.CENTER);

    this.getContentPane().add(mapTOCPanel, BorderLayout.WEST);

    7、在addEventListeners方法中,当对话框被打开的时候,在自定义状态栏中,添加一个事件监听器到自定义对话框中来安置toolbar,当它被关闭的时候恢复到正常的状态。由于这些事件不是由Java的事件调度线程生成的,对customizeCB复选框的修改应通过SwingUtilities类。见下面的代码示例:

    [Java]

    private void addEventListeners()throws IOException{

    ... customizeDialog.addICustomizeDialogEventsListener(new ICustomizeDialogEvents

    (){

    public void onStartDialog(ICustomizeDialogEventsOnStartDialogEvent arg0)

    throws IOException{

    toolbar.setCustomize(true);

    }

    public void onCloseDialog(ICustomizeDialogEventsOnCloseDialogEvent arg0)

    throws IOException{

    toolbar.setCustomize(false); java.awt.EventQueue.invokeLater(new

    Runnable(){

    public void run(){

    customizeCB.setSelected(false);

    }

    }

    );

    }

    }

    );

    ...

    8、运行该应用程序,并选择"自定义"复选框,以便将工具栏放入自定义模式,并打开"自定义"对话框。

    9、在"命令"选项卡上,单击"图形元素"类别(Graphic Element category),然后拖动"选择元素"命令(Select Elements command)以添加到toolbar(工具栏)中。右键单击toolbar(工具栏)上的一个item(项目)来调整样式和分组。看到下面的屏幕截图:

    10、关闭"自定义toolbar(工具栏)"项目对话框,以停止自定义应用程序。单击"选择工具"来移动包含今天日期的文本元素。看到下面的屏幕截图:

     

    十七、部署

    为了成功地将此应用程序部署在用户的机器上,需要创建一个可执行的JAR文件。用户可以使用一个Java运行环境(JRE)启动这个应用程序。

    做下面的步骤来创建一个可执行的JAR文件:

    1、定位到编译Java类文件的目录中,创建一个manifest.txt文件。

    2、将下面的代码添加到manifest.txt文件:

    •    Main - Class: MapViewer

    3、在第一行结束时按Enter键。

    4、保存文件并打开命令窗口。在命令提示符下,使用cd命令打开包含manifest.txt文件目录。

    5、创建一个可执行的JAR文件。见下面的代码示例:

    •    jar cmf manifest.txt mapviewer.jar *.class

    6、你创建一个可执行jar文件后,一个mapviewer.jar文件将被创建。这是可执行的可以使用命令来启动的JAR包文件。该命令可被捆绑在批处理文件或外壳脚本文件中,以提供一个启动脚本。见下面的代码示例:

    •    java -jar mapviewer.jar

     

    十八、附加资源

    使用下面的资源来理解和应用在这个场景中概述的概念和技术:

  • 为获得更多的ArcGIS开发信息,包括论坛,更新示例,和技术主题, Java资源中心。
  • 对于想要使用Java编程语言来创建应用程序的Java程序员来说,参考Java学习指南。

     

     

转载于:https://www.cnblogs.com/yimenglan/p/4688998.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值