Add graphics

Download Sample Viewer

Description

Shows how to add different graphics to a graphics layer.

Code snippet


    Point geometry = new Point(13, 55.59);
    SimpleMarkerSymbol symbol = new SimpleMarkerSymbol(Color.BLUE, 24, Style.DIAMOND);
    graphicsLayer.addGraphic(new Graphic(geometry, symbol));
  

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.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;

import com.esri.core.geometry.Envelope;
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.CompositeSymbol;
import com.esri.core.symbol.PictureFillSymbol;
import com.esri.core.symbol.PictureMarkerSymbol;
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.core.symbol.TextSymbol;
import com.esri.core.symbol.TextSymbol.HorizontalAlignment;
import com.esri.core.symbol.TextSymbol.VerticalAlignment;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.GraphicsLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.MapEvent;
import com.esri.map.MapEventListenerAdapter;

/**
 * This sample shows how to add different kinds of graphics to a graphics layer.
 */
public class AddGraphics {

  private JMap map;

  // Constructor
  public AddGraphics() { }
  
  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the map, adds a tiled basemap and a graphics layer and adds 
   * graphics to this graphics layer when the 'map ready' event fires, that is, 
   * when the map's spatial reference has been set by the basemap.
   * @return
   */
  private JMap createMap() {

    JMap jMap = new JMap();
    // set to default extent
    jMap.setExtent(new Envelope(-15.8, -37.8, 156.8, 77.3));
    
    // add base layer
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/NGS_Topo_US_2D/MapServer");
    LayerList layers = jMap.getLayers();
    layers.add(tiledLayer);
    
    // create the graphics layer
    final GraphicsLayer graphicsLayer = new GraphicsLayer();
    jMap.addMapEventListener(new MapEventListenerAdapter() {
      @Override
      public void mapReady(final MapEvent arg0) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            addSimpleMarkerGraphics(graphicsLayer);
            addPictureMarkerAndTextGraphics(graphicsLayer);
            addSimpleLineGraphics(graphicsLayer);
            addSimpleFillGraphics(graphicsLayer);
            addPictureFillGraphics(graphicsLayer);
            addCompositeSymbols(graphicsLayer);
          }
        });
      }
    });

    layers.add(graphicsLayer);

    return jMap;
  }

  /**
   * Adds graphics symbolized with SimpleMarkerSymbols.
   * @param 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));
  }

  /**
   * Adds graphics symbolized with PictureMarkerSymbols and TextSymbols.
   * @param graphicsLayer
   */
  private void addPictureMarkerAndTextGraphics(GraphicsLayer graphicsLayer) {
    String gpsNMEASentences = "$GPGGA, 92204.9, 35.6334, N, 60.2343, W, 1, 04, 2.4, 25.7, M,,,,*75\r\n"
        + "$GPGGA, 92510.5, 49.9334, N, 65.2131, W, 1, 04, 2.6, 1.7, M,,,,*75\r\n";
    String[] gpsNMEASentenceArray = gpsNMEASentences.split("\n");

    // create 2 picture marker symbols-
    // one with local image file, and another with a URL
    PictureMarkerSymbol symbol1 = null;
    BufferedImage image = null;
    URL url = this.getClass().getResource("resources/globe-16x16.png");
    try {
      image = ImageIO.read(url);
      symbol1 = new PictureMarkerSymbol(image);
      symbol1.setOffsetX(8);
      symbol1.setOffsetY(8);
    } catch (Exception e) {
      e.printStackTrace();
    }

    PictureMarkerSymbol symbol2 = new PictureMarkerSymbol(
        "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/images/86A29AC1");
    symbol2.setOffsetY(24);

    for (int i = 0; i < gpsNMEASentenceArray.length; i++) {
      String[] gpsNMEASentence = gpsNMEASentenceArray[i].split(",");

      Point geometry = new Point(Double.valueOf(gpsNMEASentence[4])
          .doubleValue(), Double.valueOf(gpsNMEASentence[2])
          .doubleValue());

      // alternate symbols
      PictureMarkerSymbol symbol = (i % 2 == 0) ? symbol1 : symbol2;

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

      TextSymbol textSymbol = new TextSymbol(25, gpsNMEASentence[9],
          Color.BLACK);
      graphic = new Graphic(geometry, textSymbol);
      graphicsLayer.addGraphic(graphic);
    }
  }

  /**
   * Adds graphics symbolized with SimpleLineSymbols.
   * @param graphicsLayer
   */
  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);

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

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

  /**
   * Adds graphics symbolized with SimpleFillSymbols.
   * @param graphicsLayer
   */
  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);
  }

  /**
   * Adds graphics symbolized with PictureFillSymbols.
   * @param graphicsLayer
   */
  private void addPictureFillGraphics(GraphicsLayer graphicsLayer) {
    Polygon polygon = new Polygon();
    polygon.startPath(100, 0);
    polygon.lineTo(110, 0);
    polygon.lineTo(110, 10);
    polygon.lineTo(100, 10);
    polygon.closePathWithLine();

    PictureFillSymbol pictureFill = new PictureFillSymbol("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/images/86A29AC1");
    Graphic graphic = new Graphic(polygon, pictureFill);
    graphicsLayer.addGraphic(graphic);
  }

  /**
   * Adds graphics symbolized with CompositeSymbols.
   * @param graphicsLayer
   */
  private void addCompositeSymbols(GraphicsLayer graphicsLayer) {
    // point with two symbols
    Point point = new Point(30, -14);
    SimpleMarkerSymbol simpleMarker = new SimpleMarkerSymbol(Color.RED, 10,
        Style.DIAMOND);
    TextSymbol text = new TextSymbol(12, "Hello", Color.BLACK);
    text.setHorizontalAlignment(HorizontalAlignment.LEFT);

    CompositeSymbol compositeSymbol = new CompositeSymbol();
    compositeSymbol.add(simpleMarker);
    compositeSymbol.add(text);

    Graphic graphic = new Graphic(point, compositeSymbol);
    graphicsLayer.addGraphic(graphic);

    // polyline from two line symbols
    Polyline polyline = new Polyline();
    polyline.startPath(28.69, -14.16);
    polyline.lineTo(14.91, -23.702);
    polyline.lineTo(1.74, -13.72);
    SimpleLineSymbol solidLine = new SimpleLineSymbol(Color.RED, 6);
    solidLine.setStyle(SimpleLineSymbol.Style.SOLID);
    SimpleLineSymbol dashLine = new SimpleLineSymbol(Color.BLACK, 2);
    dashLine.setStyle(SimpleLineSymbol.Style.DASH);

    CompositeSymbol compositeSymbolLine = new CompositeSymbol();
    compositeSymbolLine.add(solidLine);
    compositeSymbolLine.add(dashLine);

    Graphic lineGraphic = new Graphic(polyline, compositeSymbolLine);
    graphicsLayer.addGraphic(lineGraphic);

    // polygon with point, line and fill symbols
    Polygon polygon = new Polygon();
    polygon.startPath(40, -14);
    polygon.lineTo(50, -14);
    polygon.lineTo(50, -24);
    polygon.lineTo(40, -24);
    polygon.closePathWithLine();
    SimpleMarkerSymbol marker = new SimpleMarkerSymbol(Color.BLACK, 6,
        Style.TRIANGLE);
    SimpleLineSymbol line = new SimpleLineSymbol(Color.RED, 6);
    line.setAlpha(123);
    SimpleFillSymbol fill = new SimpleFillSymbol(Color.MAGENTA,
        new SimpleLineSymbol(Color.GREEN, 8));
    CompositeSymbol compositeSymbolPolygon = new CompositeSymbol();
    compositeSymbolPolygon.add(fill);
    compositeSymbolPolygon.add(line);
    compositeSymbolPolygon.add(marker);

    Graphic polygonGraphic = new Graphic(polygon, compositeSymbolPolygon);
    graphicsLayer.addGraphic(polygonGraphic);

    // visually group the above symbols
    SimpleFillSymbol groupSymbol = new SimpleFillSymbol(Color.GRAY);
    groupSymbol.setAlpha(124);
    Polygon boundingBox = new Polygon();
    boundingBox.startPath(1, -25);
    boundingBox.lineTo(51, -25);
    boundingBox.lineTo(51, -9);
    boundingBox.lineTo(1, -9);
    boundingBox.closePathWithLine();
    Graphic groupBox = new Graphic(boundingBox, groupSymbol);
    graphicsLayer.addGraphic(groupBox);

    TextSymbol compositeText = new TextSymbol(12, "Composite Symbols",
        Color.BLACK);
    compositeText.setHorizontalAlignment(HorizontalAlignment.LEFT);
    compositeText.setVerticalAlignment(VerticalAlignment.TOP);
    Point groupLabel = new Point(1.2, -9.2);
    Graphic groupText = new Graphic(groupLabel, compositeText);
    graphicsLayer.addGraphic(groupText);
  }

  // ------------------------------------------------------------------------
  // 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
          AddGraphics addGraphicsApp = new AddGraphics();

          // create the UI, including the map, for the application.
          JFrame appWindow = addGraphicsApp.createWindow();
          appWindow.add(addGraphicsApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   * 
   * @return the UI component.
   */
  public JComponent createUI() {
    // application content
    JComponent contentPane = createContentPane();

    // map
    map = createMap();

    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a content pane.
   * 
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setBounds(100, 100, 1000, 700);
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a window.
   * 
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Add Graphics Application");
    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);
        map.dispose();
      }
    });
    return window;
  }
}
Feedback on this topic?