com\esri\arcgis\sample\renderergraphics\RendererGraphics.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 com.esri.arcgis.sample.renderergraphics; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import com.esri.adf.web.data.GraphicElement; import com.esri.adf.web.data.GraphicRenderer; import com.esri.adf.web.data.WebContext; import com.esri.adf.web.data.WebGraphics; import com.esri.adf.web.data.WebMap; import com.esri.adf.web.data.geometry.WebExtent; import com.esri.adf.web.data.geometry.WebGeometry; import com.esri.adf.web.data.geometry.WebMultiPoint; import com.esri.adf.web.data.geometry.WebPoint; import com.esri.adf.web.data.geometry.WebPolygon; import com.esri.adf.web.data.geometry.WebPolyline; import com.esri.adf.web.data.geometry.WebPath; import com.esri.adf.web.data.query.WebQuery; import com.esri.adf.web.data.results.WebResultsToc; import com.esri.adf.web.data.symbol.WebSimpleLineSymbol; import com.esri.adf.web.data.symbol.WebSimpleMarkerSymbol; import com.esri.adf.web.data.symbol.WebSimplePolygonSymbol; import com.esri.adf.web.faces.event.MapEvent; import com.esri.adf.web.data.renderer.WebRenderer; import com.esri.adf.web.data.renderer.WebUniqueValueRenderer; import com.esri.adf.web.data.renderer.WebClassBreaksRenderer; import com.esri.adf.web.data.renderer.WebSimpleRenderer; import com.esri.adf.web.data.renderer.WebClassBreakInfo; import com.esri.adf.web.data.renderer.WebUniqueValueInfo; public class RendererGraphics { WebContext context = null; WebQuery query = null; WebGeometry geom = null; GraphicElement graphicElem = null; int width = 400; int height = 400; double x = 0; double y = 0; public void addWebGraphics(MapEvent event){ WebGeometry geom = event.getWebGeometry(); GraphicElement element = new GraphicElement(); if (geom instanceof WebPoint){ WebPoint point = (WebPoint)event.getWebGeometry(); WebMultiPoint points = new WebMultiPoint(); points.addPoint(point); WebSimpleMarkerSymbol markerSymbol = new WebSimpleMarkerSymbol(); markerSymbol.setAngle(0); Random rand = new Random(); String colorStr = (rand.nextInt(255)+1) + "," + (rand.nextInt(255)+1) + "," + (rand.nextInt(255)+1); markerSymbol.setColor(colorStr); markerSymbol.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); markerSymbol.setOutlineColor("0,0,0"); markerSymbol.setTransparency(1.0); markerSymbol.setWidth(10); element.setSymbol(markerSymbol); element.setGeometry(points.toMapGeometry(context.getWebMap())); } else if (geom instanceof WebPolyline){ WebPolyline polyline = (WebPolyline) event.getWebGeometry(); WebSimpleLineSymbol lineSymbol = new WebSimpleLineSymbol(); lineSymbol.setAntialiasing(true); lineSymbol.setCapType(WebSimpleLineSymbol.ROUND); lineSymbol.setColor("0,0,0"); lineSymbol.setJoinType(WebSimpleLineSymbol.ROUND); lineSymbol.setLineType(WebSimpleLineSymbol.SOLID); lineSymbol.setTransparency(1.0); lineSymbol.setWidth(2); element.setSymbol(lineSymbol); element.setGeometry(polyline.toMapGeometry(context.getWebMap())); }else if (geom instanceof WebPolygon){ WebPolygon polygon = (WebPolygon)event.getWebGeometry(); WebSimplePolygonSymbol polygonSymbol = new WebSimplePolygonSymbol(); polygonSymbol.setBoundaryColor("0,255,0"); polygonSymbol.setBoundaryWidth(3); polygonSymbol.setFillType(WebSimplePolygonSymbol.SOLID); polygonSymbol.setFillColor("255,0,0"); polygonSymbol.setFillTransparency(1.0); polygonSymbol.setAntialiasing(true); element.setSymbol(polygonSymbol); element.setGeometry(polygon.toMapGeometry(context.getWebMap())); } WebGraphics graphics = context.getWebGraphics(); graphics.addGraphics(element); } public void addRenderGraphics(MapEvent event){ int renderer = 2; GraphicRenderer gRenderer = new GraphicRenderer(); WebRenderer wr = null; String uniqueValues[] = {"1", "2", "3", "4", "5", "6"}; WebSimpleMarkerSymbol defaultMarkerSymbol = new WebSimpleMarkerSymbol(); defaultMarkerSymbol.setColor("0,0,255"); defaultMarkerSymbol.setWidth(25); defaultMarkerSymbol.setMarkerType(WebSimpleMarkerSymbol.STAR); if (renderer == 1){ WebSimpleRenderer wsr = new WebSimpleRenderer(); wsr.setSymbol(defaultMarkerSymbol); wr = wsr; }else if (renderer == 2){ defaultMarkerSymbol.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); WebUniqueValueRenderer wuvr = new WebUniqueValueRenderer(); wuvr.setDefaultSymbol(defaultMarkerSymbol); wuvr.setDefaultLabel("Default Label"); wuvr.setField1("TYPE"); wuvr.setField2(null); wuvr.setField3(null); wuvr.setFieldDelimiter(null); ArrayList infos = new ArrayList(5); WebSimpleMarkerSymbol redMS = new WebSimpleMarkerSymbol(); redMS.setColor("255,0,0"); redMS.setWidth(15); redMS.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); WebUniqueValueInfo uvi = new WebUniqueValueInfo("Red Color", "Red Color", "1", redMS); infos.add(uvi); WebSimpleMarkerSymbol greenMS = new WebSimpleMarkerSymbol(); greenMS.setColor("0,255,0"); greenMS.setWidth(25); greenMS.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); uvi = new WebUniqueValueInfo("Green Color", "Green Color", "2", greenMS); infos.add(uvi); WebSimpleMarkerSymbol blueMS = new WebSimpleMarkerSymbol(); blueMS.setColor("0,0,255"); blueMS.setWidth(35); blueMS.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); uvi = new WebUniqueValueInfo("Blue Color", "Blue Color", "3", blueMS); infos.add(uvi); WebSimpleMarkerSymbol yellowMS = new WebSimpleMarkerSymbol(); yellowMS.setColor("255,255,0"); yellowMS.setWidth(45); yellowMS.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); uvi = new WebUniqueValueInfo("Yellow Color", "Yellow Color", "4", yellowMS); infos.add(uvi); WebSimpleMarkerSymbol cyanMS = new WebSimpleMarkerSymbol(); cyanMS.setColor("0,255,255"); cyanMS.setWidth(55); cyanMS.setMarkerType(WebSimpleMarkerSymbol.CIRCLE); uvi = new WebUniqueValueInfo("Cyan Color", "Cyan Color", "5", cyanMS); infos.add(uvi); wuvr.setUniquValueInfos(infos); wr = wuvr; }else if (renderer == 3){ WebClassBreaksRenderer wcbr = new WebClassBreaksRenderer(); wcbr.setDefaultSymbol(defaultMarkerSymbol); wcbr.setField("Default Label"); wcbr.setMinimumValue(0); ArrayList infos = new ArrayList(5); WebSimpleMarkerSymbol redMS = new WebSimpleMarkerSymbol(); redMS.setColor("255,0,0"); redMS.setWidth(15); redMS.setMarkerType(WebSimpleMarkerSymbol.STAR); WebClassBreakInfo cbi = new WebClassBreakInfo("Red Color", "Red Color", 5, redMS); infos.add(cbi); WebSimpleMarkerSymbol greenMS = new WebSimpleMarkerSymbol(); greenMS.setColor("0,255,0"); greenMS.setWidth(25); greenMS.setMarkerType(WebSimpleMarkerSymbol.STAR); cbi = new WebClassBreakInfo("Green Color", "Green Color", 10, greenMS); infos.add(cbi); WebSimpleMarkerSymbol blueMS = new WebSimpleMarkerSymbol(); blueMS.setColor("0,0,255"); blueMS.setWidth(35); blueMS.setMarkerType(WebSimpleMarkerSymbol.STAR); cbi = new WebClassBreakInfo("Blue Color", "Blue Color", 15, blueMS); infos.add(cbi); WebSimpleMarkerSymbol yellowMS = new WebSimpleMarkerSymbol(); yellowMS.setColor("255,255,0"); yellowMS.setWidth(45); yellowMS.setMarkerType(WebSimpleMarkerSymbol.STAR); cbi = new WebClassBreakInfo("Yellow Color", "Yellow Color", 20, yellowMS); infos.add(cbi); WebSimpleMarkerSymbol cyanMS = new WebSimpleMarkerSymbol(); cyanMS.setColor("0,255,255"); cyanMS.setWidth(55); cyanMS.setMarkerType(WebSimpleMarkerSymbol.STAR); cbi = new WebClassBreakInfo("Cyan Color", "Cyan Color", 25, cyanMS); infos.add(cbi); wcbr.setClassBreakInfos(infos); wr = wcbr; } ArrayList geometries = new ArrayList(); ArrayList values = new ArrayList(); WebGeometry geom = event.getWebGeometry(); WebPoint point = null; Random randVal = new Random(); if (geom instanceof WebPoint){ point = (WebPoint)event.getWebGeometry(); geometries.add(point.toMapGeometry(context.getWebMap())); values.add(new Integer(randVal.nextInt(4) + 1)); } else if (geom instanceof WebPolyline){ WebPolyline polyline = (WebPolyline)event.getWebGeometry(); WebPath path = null; for (Iterator iter = polyline.getPaths().iterator(); iter.hasNext(); ) { path = (WebPath)iter.next(); for (Iterator iter1 = path.getPoints().iterator(); iter1.hasNext(); ) { geometries.add(((WebPoint)iter1.next()).toMapGeometry(context.getWebMap())); if (renderer == 2){ int mynum = new Integer(randVal.nextInt(4) + 1); String mystr = Integer.toString(mynum); values.add(mystr); }else if (renderer == 3){ int v = randVal.nextInt(4) + 1; values.add(uniqueValues[v]); } } } } gRenderer.setRenderer(wr); gRenderer.setGeometries(geometries); for (Iterator iterVal = values.iterator(); iterVal.hasNext(); ) { System.out.println(iterVal.next()); } gRenderer.setValues(values); drawWebGraphics(gRenderer); } public void drawWebGraphics(GraphicRenderer renderer){ WebGraphics graphics = context.getWebGraphics(); graphics.addRenderer(renderer); context.refresh(); } public void clearWebGraphics(){ WebGraphics graphics = context.getWebGraphics(); graphics.clearGraphics(); context.refresh(); } public void resize() { WebMap wMap = context.getWebMap(); WebExtent oldExtent = new WebExtent(wMap.getCurrentExtent()); double mx1 = oldExtent.getMinX(); double my1 = oldExtent.getMinY(); double mx2 = oldExtent.getMaxX(); double my2 = oldExtent.getMaxY(); double mx3 = ((Math.abs(mx2-mx1) / wMap.getWidth()) * getWidth()) + mx1 ; double my3 = -((((my2-my1) / wMap.getHeight()) * getHeight()) - my2); wMap.setHeight(getHeight()); wMap.setWidth(getWidth()); WebExtent newExtent = new WebExtent(oldExtent.getMinX(), my3, mx3, oldExtent.getMaxY() ,context.getSpatialReference()); wMap.setCurrentExtent(newExtent); context.refresh(); } public WebContext getContext() { return context; } public void setContext(WebContext context) { this.context = context; } public WebGeometry getGeom() { return geom; } public int getWidth() { return width; } public int getHeight() { return height; } public double getY() { if (geom != null) return ((WebPoint)geom).getY(); else return y; } public double getX() { if (geom != null) return ((WebPoint)geom).getX(); else return x; } public void setGeom(WebGeometry geom) { this.geom = geom; } public void setWidth(int width) { this.width = width; } public void setHeight(int height) { this.height = height; } public void setY(double y) { this.y = y; } public void setX(double x) { this.x = x; } }