arcgissamples\defensesolutions\MOLEBasicDisplay.java
/* Copyright 2010 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. * */ package arcgissamples.defensesolutions; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.net.UnknownHostException; import java.util.Random; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; 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.defensesolutions.IMoleSymbol; import com.esri.arcgis.defensesolutions.MoleMarkerSymbol; import com.esri.arcgis.display.IColor; import com.esri.arcgis.display.IMarkerSymbol; import com.esri.arcgis.display.IRgbColor; import com.esri.arcgis.display.IScreenDisplay; import com.esri.arcgis.display.ISymbol; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.systemUI.esriCommandStyles; public class MOLEBasicDisplay extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; static JPanel mainPanel = null; static JPanel rightPanel = null; static JButton addGraphicButton = null; static JButton removeGraphicButton = null; static MapBean mapBean; static TOCBean tocBean; static ToolbarBean toolbarBean; IScreenDisplay sc; String[] sic = {"SFAPC----------", "SFAPCF---------", "SFAPFH---------", "SFAPCL---------", "SFAPM----------"}; Random m = new Random(); Random n = new Random(); public MOLEBasicDisplay() throws Exception { super("Add FeatureLayer"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { new AoInitialize().shutdown(); } catch(Exception ex) { // exit anyway } System.exit(0); } }); buildFrame(); String arcgisHome = null; String dataPath = null; arcgisHome = System.getenv("AGSDEVKITJAVA"); if (arcgisHome == null) { System.out.println( "AGSDEVKITJAVA environment variable is not set, exiting application"); System.exit(-1); } dataPath = arcgisHome + java.io.File.separator + "java" + java.io.File.separator + "samples" + java.io.File.separator + "data" + java.io.File.separator + "mole" + java.io.File.separator + "mole_base.mxd"; mapBean.loadMxFile( dataPath, null, null); sc = mapBean.getActiveView().getScreenDisplay(); setSize(650, 500); setVisible(true); try { initControl(); } catch (Exception e1) { System.out.println("Couldn't initialize sample application."); throw e1; } } public static void main(String[] s) { try { EngineInitializer.initializeVisualBeans(); new AoInitialize().initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); new MOLEBasicDisplay(); } catch (Exception e) { System.out.println("Error in sample application: " + e.getMessage()); e.printStackTrace(); System.out.println("Exiting ..."); } } public void buildFrame() { rightPanel = new JPanel(); mainPanel = new JPanel(); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); addGraphicButton = new JButton("Add Graphic"); addGraphicButton.addActionListener(this); removeGraphicButton = new JButton("Remove Graphic"); removeGraphicButton.addActionListener(this); rightPanel.add(addGraphicButton); rightPanel.add(Box.createVerticalStrut(10)); rightPanel.add(removeGraphicButton); rightPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mapBean = new MapBean(); tocBean = new TOCBean(); toolbarBean = new ToolbarBean(); tocBean.setSize(new Dimension(200, 100)); toolbarBean.setSize(490, 20); mainPanel.setLayout(new BorderLayout()); mainPanel.add(tocBean, BorderLayout.WEST); mainPanel.add(rightPanel, BorderLayout.EAST); mainPanel.add(toolbarBean, BorderLayout.NORTH); mainPanel.add(mapBean, BorderLayout.CENTER); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); getContentPane().add(mainPanel, BorderLayout.CENTER); } /** * Initialize control * @throws Exception if couldn't add command or tool to toolbar bean */ public void initControl() throws Exception { try { //Set the Buddy toolbarBean.setBuddyControl(mapBean); tocBean.setBuddyControl(mapBean); //Add tool bar items.. toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //open toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomIn toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomOut toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //Pan toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleTextOnly); } catch (Exception e) { System.out.println("Couldn't add commands or tools to toolbar bean."); throw e; } } /** * @see java.awt.event.ActionListener#actionPerformed(ActionEvent event) * @param event */ public void actionPerformed(ActionEvent event) { if(event.getSource() == addGraphicButton) { drawSymbols(); } if(event.getSource() == removeGraphicButton) { try { if(sc!=null) { sc.invalidate(mapBean.getExtent(), true, (short)com.esri.arcgis.display.esriDisplayCacheFlags.esriDisplayCacheNoFlags ); } } catch (Exception e) { System.out.println(e.getMessage()); } } } /** * Method draws the mole symbols to IDisplay * */ /** * */ public void drawSymbols() { try { sc.startDrawing(0,(short)com.esri.arcgis.display.esriDisplayCacheFlags.esriDisplayCacheNoFlags ); for(int i=0; i<sic.length ; i++){ IMoleSymbol markerSymbol = new MoleMarkerSymbol(); markerSymbol.setSymbolID(sic[i].toString()); System.out.println(sic[i]); IPoint point = new Point(); point = createRandomPoint(); IMarkerSymbol mm = (IMarkerSymbol)markerSymbol; mm.setColor(createRandomColor()); sc.setSymbol((ISymbol)mm); sc.drawPoint(point); } sc.finishDrawing(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public IPoint createRandomPoint() { IPoint point = null; try { point = new Point(); double XX = -180 + m.nextFloat()* 360; double YY = -90 + m.nextFloat()* 180; point.putCoords(XX , YY ); System.out.println(XX+" "+YY); System.out.println(point.getX()+" "+point.getY()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return point; } public IColor createRandomColor() { IRgbColor color = null; try { color = new RgbColor(); color.setRed(m.nextInt()); color.setGreen(m.nextInt()); color.setBlue(m.nextInt()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return color; } /** * Main program to start the program execution. */ }