arcgissamples\cartography\BivariateRenderers.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.cartography; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.UIManager; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.ClassBreaksRenderer; import com.esri.arcgis.carto.IFeatureRenderer; import com.esri.arcgis.carto.IGeoFeatureLayer; import com.esri.arcgis.carto.ILayer; import com.esri.arcgis.carto.esriViewDrawPhase; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleFillSymbol; import com.esri.arcgis.display.esriSimpleFillStyle; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; /** * This sample demonstrates custom color bivariate renderers. * The first two layers are original. They store graduated color symbols. * The third layer has a color bivariate renderer that represents a combination * of two previous layers. */ public class BivariateRenderers extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; static AoInitialize aoInit; MapBean mapBean = new MapBean(); TOCBean tocBean = new TOCBean(); Button buttonOriginalRenderers = new Button(); Button buttonBivariateRenderer = new Button(); ClassBreaksRenderer classBreaksRenderer0; // renderer for first layer ClassBreaksRenderer classBreaksRenderer1; // renderer for second layer /** * Method to start the program execution. */ public static void main(String s[]) { System.out.println("Starting BivariateRenderers - An ArcObjects Java SDK Developer Sample"); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); if (!(new File(devKitHome).exists())) { System.out.println(devKitHome + " does not exist.\nExiting..."); System.exit(-1); } // // Change the following lines if you want to use different data // String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; String featureClassName = "states"; File shapefileFile = new File(workspacePath, featureClassName + ".shp"); if (!shapefileFile.exists()) { System.out.println("Shapefile does not exist: " + shapefileFile.getAbsolutePath()); System.exit(0); } try { EngineInitializer.initializeVisualBeans(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); BivariateRenderers thisApp = new BivariateRenderers(); thisApp.initUI(workspacePath, featureClassName); } catch (Exception ex) { System.out.println("Sample failed: " + ex.getMessage()); ex.printStackTrace(); } } static void initializeArcGISLicenses() { try { aoInit = new AoInitialize(); if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } catch (Exception e) {e.printStackTrace();} } /* * Lays out the User Interface, and sets up event listeners */ private void initUI(String path, String name) throws AutomationException, IOException { this.setTitle("BivariateRenderers Sample Application"); this.setSize(new Dimension(1200, 800)); this.getContentPane().setLayout(new BorderLayout()); Panel rendererPanel = new Panel(new FlowLayout(FlowLayout.LEADING)); this.buttonOriginalRenderers.setLabel("Set original renderers for layers 1 and 2"); this.buttonBivariateRenderer.setLabel("Set Bivariate Renderer for layer 3 (combination of 1 and 2)"); rendererPanel.add(this.buttonOriginalRenderers); rendererPanel.add(this.buttonBivariateRenderer); Panel toolPanel = new Panel(new GridLayout(2,1)); toolPanel.add(rendererPanel); Panel nestPanel = new Panel(); nestPanel.add(toolPanel); this.getContentPane().add(this.mapBean, BorderLayout.CENTER); this.getContentPane().add(this.tocBean, BorderLayout.WEST); this.getContentPane().add(nestPanel, BorderLayout.SOUTH); this.setVisible(true); // set listeners this.buttonBivariateRenderer.addActionListener(this); this.buttonBivariateRenderer.setEnabled(false); this.buttonOriginalRenderers.addActionListener(this); this.mapBean.clearLayers(); this.mapBean.addShapeFile(path, name); // first layer this.mapBean.addShapeFile(path, name); // second layer this.mapBean.addShapeFile(path, name); // third layer // update TOC and map this.setVisible(true); this.tocBean.setBuddyControl(this.mapBean); this.tocBean.update(); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { aoInit.shutdown(); } catch(IOException ex) { // exit anyway } System.exit(0); } }); } /** * Method implements interface ActionListener * @see ActionListener#actionPerformed * @param event */ public void actionPerformed(ActionEvent event) { Object eventSource = event.getSource(); // Set Original Renderers try { if (eventSource == this.buttonOriginalRenderers) { // prepare graduated color renderer for first layer in green gradation by population field double classes[] = new double[4]; classes[0] = 750000.0; classes[1] = 2000000.0; classes[2] = 10000000.0; classes[3] = 100000000.0; this.classBreaksRenderer0 = new ClassBreaksRenderer(); this.classBreaksRenderer0.setField("POP1999"); this.classBreaksRenderer0.setBreakCount(4); this.classBreaksRenderer0.setSortClassesAscending(true); for (int i0 = 0; i0 < 4; i0++) { RgbColor color = new RgbColor(); color.setGreen((i0+1) * 64 - 1); SimpleFillSymbol fillSymbol = new SimpleFillSymbol(); fillSymbol.setColor(color); fillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid); this.classBreaksRenderer0.setSymbol(i0, fillSymbol); this.classBreaksRenderer0.setBreak(i0, classes[i0]); } ILayer layer0 = this.mapBean.getLayer(0); IGeoFeatureLayer geoFeatureLayer0 = (IGeoFeatureLayer)(layer0); IFeatureRenderer featureRenderer = this.classBreaksRenderer0; geoFeatureLayer0.setRendererByRef(featureRenderer); // prepare graduated color renderer for second layer in blue gradation by area field double classes1[] = new double[4]; classes1[0] = 10000.0; classes1[1] = 50000.0; classes1[2] = 100000.0; classes1[3] = 1000000.0; this.classBreaksRenderer1 = new ClassBreaksRenderer(); this.classBreaksRenderer1.setField("AREA"); this.classBreaksRenderer1.setBreakCount(4); this.classBreaksRenderer1.setSortClassesAscending(true); for (int i1 = 0; i1 < 4; i1++) { RgbColor color = new RgbColor(); color.setBlue((i1+1) * 64 - 1); SimpleFillSymbol fillSymbol = new SimpleFillSymbol(); fillSymbol.setColor(color); fillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid); this.classBreaksRenderer1.setSymbol(i1, fillSymbol); this.classBreaksRenderer1.setBreak(i1, classes1[i1]); } ILayer layer1 = this.mapBean.getLayer(1); IGeoFeatureLayer geoFeatureLayer1 = (IGeoFeatureLayer)(layer1); IFeatureRenderer featureRenderer1 = this.classBreaksRenderer1; geoFeatureLayer1.setRendererByRef(featureRenderer1); // update TOC and map this.tocBean.update(); this.mapBean.getActiveView().refresh(); this.buttonBivariateRenderer.setEnabled(true); } } catch (java.io.IOException ex) { System.out.println("Can't set Bivariate Renderer, exception :" + ex); } // Set BivariateRenderer try { if (eventSource == this.buttonBivariateRenderer) { ColorBivariateRend colorBivariateRend = new ColorBivariateRend(); IFeatureRenderer featureRenderer0 = this.classBreaksRenderer0; colorBivariateRend.setMainRendererByRef(featureRenderer0); colorBivariateRend.setMainRendererByRef(this.classBreaksRenderer0); IFeatureRenderer featureRenderer1 = this.classBreaksRenderer1; colorBivariateRend.setVariationRendererByRef(featureRenderer1); colorBivariateRend.createLegend(); ILayer layer2 = this.mapBean.getLayer(2); IGeoFeatureLayer geoFeatureLayer2 = (IGeoFeatureLayer)(layer2); geoFeatureLayer2.setRendererByRef(colorBivariateRend); // update TOC and map this.tocBean.update(); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } } catch (java.io.IOException ex) { System.out.println("Can't set Bivariate Renderer, exception :" + ex); } } }