Statically rendered graphics layer

Download Sample Viewer

Description

Shows how to add a statically rendered graphics layer and feature layer. In the left map, notice how the statically rendered graphics resize abruptly when you zoom in and out; the dynamically rendered graphics animate smoothly when you zoom in and out.

Code snippet


  // create a statically rendered (local) feature layer
  ArcGISLocalFeatureLayer featureLayer = 
    new ArcGISLocalFeatureLayer(getPathSampleData() + "mpks/USTaxiStates.mpk", 0,
      RenderingMode.STATIC));
  
  // create a statically rendered graphics layer
  GraphicsLayer graphicsLayer = new GraphicsLayer(RenderingMode.STATIC);
  

Sample Code

/* Copyright 2014 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 com.esri.client.samples.graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.client.local.ArcGISLocalFeatureLayer;
import com.esri.core.geometry.MultiPoint;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.GraphicsLayer;
import com.esri.map.GraphicsLayer.RenderingMode;
import com.esri.map.JMap;
import com.esri.map.MapEvent;
import com.esri.map.MapEventListenerAdapter;
import com.esri.runtime.ArcGISRuntime;

/***
 * This sample demonstrates how a statically rendered graphics layer behaves.
 */
public class StaticRenderedGraphicsLayer {
  // resources
  final String URL = "http://services.arcgisonline.com/ArcGIS/rest/services/NGS_Topo_US_2D/MapServer";

  // JMap
  private JMap staticCanvasLayer;
  private JMap dynamicGraphicsLayer;

  // ------------------------------------------------------------------------
  // Constructors
  // ------------------------------------------------------------------------
  public StaticRenderedGraphicsLayer() {
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * 
   * @param args
   *            arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          StaticRenderedGraphicsLayer staticGraphicLayerApp = new StaticRenderedGraphicsLayer();
          // create the UI, including the maps, for the application.
          JFrame appWindow = staticGraphicLayerApp.createWindow();
          appWindow.add(staticGraphicLayerApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------

  /**
   * Creates and displays the UI, including the static feature layer, for this
   * application.
   */
  public JComponent createUI() throws Exception {

    // content pane
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, 0));

    // map with a static canvas layer
    staticCanvasLayer = new JMap();
    staticCanvasLayer.setBorder(new LineBorder(new Color(0, 0, 0, 100), 1));
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(
        URL);
    staticCanvasLayer.getLayers().add(baseLayer);
    staticCanvasLayer.getLayers().add(
        new ArcGISLocalFeatureLayer(getPathSampleData() + "mpks/USTaxiStates.mpk", 0,
            RenderingMode.STATIC));
    addGraphics(staticCanvasLayer, RenderingMode.STATIC);

    // map with a dynamic graphics layer
    // this map has the same content as above, but the local feature layer
    // is rendered with a default graphics layer
    dynamicGraphicsLayer = new JMap();
    dynamicGraphicsLayer.setBorder(new LineBorder(new Color(0, 0, 0, 100),
        1));
    final ArcGISTiledMapServiceLayer baseLayer2 = new ArcGISTiledMapServiceLayer(
        URL);
    dynamicGraphicsLayer.getLayers().add(baseLayer2);
    dynamicGraphicsLayer.getLayers().add(
        new ArcGISLocalFeatureLayer(getPathSampleData() + "mpks/USTaxiStates.mpk", 0));
    addGraphics(dynamicGraphicsLayer, RenderingMode.DYNAMIC);

    contentPane.add(staticCanvasLayer);
    JComponent space = (JComponent) Box
        .createRigidArea(new Dimension(1, 5));
    contentPane.add(space);
    contentPane.add(dynamicGraphicsLayer);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * create the same graphics for both maps on either a static or dynamic
   * graphic layer
   * 
   * @param map
   * @param mode
   */
  private void addGraphics(JMap jMap, final RenderingMode mode) {

    jMap.addMapEventListener(new MapEventListenerAdapter() {
      @Override
      public void mapReady(final MapEvent arg0) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            final GraphicsLayer graphicsLayer = new GraphicsLayer(
                mode);
            ((JMap) arg0.getSource()).getLayers()
            .add(graphicsLayer);

            addSimpleMarkerGraphics(graphicsLayer);
            addSimpleLineGraphics(graphicsLayer);
            addSimpleFillGraphics(graphicsLayer);
          }
        });
      }
    });
  }

  private void addSimpleMarkerGraphics(GraphicsLayer graphicsLayer) {
    // points
    Point point1 = new Point(13, 55.59);
    Point point2 = new Point(72.83, 18.97);
    Point point3 = new Point(5.43, 34.3);

    SimpleMarkerSymbol blueDiamond = new SimpleMarkerSymbol(Color.BLUE, 24,
        Style.DIAMOND);
    SimpleMarkerSymbol redCircle = new SimpleMarkerSymbol(Color.RED, 16,
        Style.CIRCLE);
    SimpleMarkerSymbol blackTriangle = new SimpleMarkerSymbol(Color.BLACK,
        16, Style.TRIANGLE);

    graphicsLayer.addGraphic(new Graphic(point1, blueDiamond));
    graphicsLayer.addGraphic(new Graphic(point2, redCircle));
    graphicsLayer.addGraphic(new Graphic(point3, blackTriangle));

    // multipoint
    MultiPoint mp = new MultiPoint();
    Point p1 = new Point(50, 50);
    Point p2 = new Point(50, 52);
    Point p3 = new Point(52, 50);
    Point p4 = new Point(52, 52);
    mp.add(p1);
    mp.add(p2);
    mp.add(p3);
    mp.add(p4);
    SimpleMarkerSymbol mpSymbol = new SimpleMarkerSymbol(Color.DARK_GRAY,
        12, Style.CIRCLE);
    graphicsLayer.addGraphic(new Graphic(mp, mpSymbol));
  }

  private void addSimpleLineGraphics(GraphicsLayer graphicsLayer) {
    Polyline polyline = new Polyline();
    polyline.startPath(118.169, 34.016);
    polyline.lineTo(104.941, 39.7072);
    polyline.lineTo(96.724, 32.732);

    Polyline polyline2 = new Polyline();
    polyline2.startPath(28.69, -14.16);
    polyline2.lineTo(14.91, -23.702);
    polyline2.lineTo(1.74, -13.72);

    SimpleLineSymbol symbol = new SimpleLineSymbol(Color.MAGENTA, 4);
    symbol.setStyle(SimpleLineSymbol.Style.DASH_DOT);

    Graphic graphic = new Graphic(polyline, symbol);
    graphicsLayer.addGraphic(graphic);

    graphic = new Graphic(polyline2, symbol);
    graphicsLayer.addGraphic(graphic);
  }

  private void addSimpleFillGraphics(GraphicsLayer graphicsLayer) {
    Polygon polygon = new Polygon();
    polygon.startPath(25, 5.59);
    polygon.lineTo(13.42, 3.92);
    polygon.lineTo(12.3, 23.3);
    polygon.lineTo(38.2, 22.9);
    polygon.closePathWithLine();

    SimpleLineSymbol outline = new SimpleLineSymbol(new Color(0, 200, 0),
        3, SimpleLineSymbol.Style.DASH_DOT_DOT);
    SimpleFillSymbol symbol = new SimpleFillSymbol(
        new Color(200, 0, 0, 120), outline);
    Graphic graphic = new Graphic(polygon, symbol);
    graphicsLayer.addGraphic(graphic);

    Polygon polygon2 = new Polygon();
    polygon2.startPath(80, 0);
    polygon2.lineTo(90, 0);
    polygon2.lineTo(90, 10);
    polygon2.lineTo(80, 10);
    polygon2.closePathWithLine();

    SimpleLineSymbol outline2 = new SimpleLineSymbol(new Color(0, 0, 0), 3);
    SimpleFillSymbol symbol2 = new SimpleFillSymbol(new Color(165, 42, 42,
        255), outline2, SimpleFillSymbol.Style.DIAGONAL_CROSS);
    Graphic graphic2 = new Graphic(polygon2, symbol2);
    graphicsLayer.addGraphic(graphic2);
  }

  /**
   * Creates a window.
   * 
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Static Graphic Layer");
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        staticCanvasLayer.dispose();
        dynamicGraphicsLayer.dispose();
      }
    });
    return window;
  }

  private String getPathSampleData() {
    String FSP = System.getProperty("file.separator");
    String dataPath = null;
    String javaPath = ArcGISRuntime.getInstallDirectory();
    if (javaPath != null) {
      if (!(javaPath.endsWith("/") || javaPath.endsWith("\\"))){
        javaPath += FSP;
      }
      dataPath = javaPath + "sdk" + FSP + "samples" + FSP + "data" + FSP;
    }
    File dataFile = new File(dataPath);
    if (!dataFile.exists()) { 
      dataPath = ".." + FSP + "data" + FSP;
    }
    return dataPath;
  }
}
Feedback on this topic?