arcgissamples\mapbean\DrawText.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.mapbean; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.SimpleRenderer; import com.esri.arcgis.carto.esriViewDrawPhase; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnAfterDrawEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent; import com.esri.arcgis.display.BezierTextPath; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleFillSymbol; import com.esri.arcgis.display.SimpleLineSymbol; import com.esri.arcgis.display.TextSymbol; import com.esri.arcgis.geometry.IEnvelope; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.geometry.Polyline; import com.esri.arcgis.support.ms.stdole.StdFont; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; /** * Description: This draw text sample application demonstrates the splined text rendering capabilities of ArcObjects. * The user clicks on the map to track a line, and the MapBean method DrawText is used to place the text within the * TextBox along the tracked line. Note that at least two clicks are required for the line and text to appear on the * MapBean. * <p> * The application uses the AddShapeFile method to load in the world countries sample data which is then symbolized. The * OnClick event uses either the TrackRectangle method to zoom in if the right or middle mouse button has been used, or * adds the new point to the line and updates the display through the refresh method. The refresh method is used to draw * the esriViewForeground, thereby removing any previously drawn annotation, and triggering the MapBean's OnAfterDraw * event. The OnAfterDraw event uses the DrawShape and DrawText methods to draw the line and text onto the MapBean in * response to the refresh of the esriViewForeground phase. */ public class DrawText extends JFrame implements ActionListener { static AoInitialize aoInit; JPanel mainPanel = null; JPanel bottomPanel = null; JPanel rightPanel = null; JButton clearButton = null; JButton fullExtentButton = null; JLabel helpLabel = null; JPanel buttonPanel = null; JLabel enterTextLabel = null; JTextField textField = null; // Find sample data by navigating two folders up String filePath = ""; String helpLabelString = "<HTML> Left mouse <BR>" + "button to trace a line <BR>" + "to draw text along. <BR>" + "<BR>" + "Right mouse button to <BR>" + "drag a rectangle to zoom in.. <BR>" + "</HTML>"; MapBean mapBean = null; Polyline polyLine = null; public DrawText() { super("Draw Text Example"); buildFrame(); setSize(500, 400); setVisible(true); initControl(); } public void buildFrame() { mainPanel = new JPanel(); bottomPanel = new JPanel(); rightPanel = new JPanel(); // Create map control and it to the center of the main panel. mapBean = new MapBean(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(mapBean, BorderLayout.CENTER); helpLabel = new JLabel(helpLabelString); clearButton = new JButton("Clear Text"); fullExtentButton = new JButton("Full Extent"); clearButton.addActionListener(this); fullExtentButton.addActionListener(this); buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); buttonPanel.add(clearButton, BorderLayout.NORTH); buttonPanel.add(fullExtentButton, BorderLayout.SOUTH); rightPanel.setLayout(new BorderLayout()); rightPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); rightPanel.add(helpLabel, BorderLayout.NORTH); rightPanel.add(buttonPanel, BorderLayout.SOUTH); enterTextLabel = new JLabel("Enter Text"); textField = new JTextField("Put a map in your app..."); bottomPanel = new JPanel(); bottomPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 160)); bottomPanel.setLayout(new GridLayout(2, 1)); bottomPanel.add(enterTextLabel); bottomPanel.add(textField); mainPanel.add(rightPanel, BorderLayout.EAST); getContentPane().add(mainPanel, BorderLayout.CENTER); getContentPane().add(bottomPanel, BorderLayout.SOUTH); } /** * Method which initializes the map with the shape data and adds symbology to map. */ public void initControl() { try { FeatureLayer featureLayer = null; SimpleRenderer simpleRenderer = null; SimpleFillSymbol simpleFillSymbol = null; SimpleLineSymbol simpleLineSymbol = null; String devKitHome = null; try { //Get DEVKITHOME Home devKitHome = System.getenv("AGSDEVKITJAVA"); } catch (Error e) { devKitHome = JOptionPane.showInputDialog("Please enter the path to the ArcGIS developer kit location."); } filePath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "world"; // Add sample 'dissolvecntry' shapefile data mapBean.addShapeFile(filePath, "continent.shp"); // Create a Featurelayer object on the layer // in the map control in order to symbolize the data featureLayer = (FeatureLayer) mapBean.getLayer(0); // Create a simple renderer object simpleRenderer = new SimpleRenderer(); // Create a fill symbol simpleFillSymbol = new SimpleFillSymbol(); // Create a line symbol simpleLineSymbol = new SimpleLineSymbol(); // Assign line symbol and fill symbol properties simpleLineSymbol.setWidth(0.1); simpleLineSymbol.setColor(getRGBColor(255, 0, 0)); simpleFillSymbol.setOutline(simpleLineSymbol); simpleFillSymbol.setColor(getRGBColor(0, 0, 255)); // Set the symbol property of the renderer simpleRenderer.setSymbolByRef(simpleFillSymbol); // Set the renderer property of the geo feature layer featureLayer.setRendererByRef(simpleRenderer); // Refresh map mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null); // Add map control listener mapBean.addIMapControlEvents2Listener(new MapControlListener()); } catch (Exception ex) { System.out.println("Exception in initMap : " + ex); ex.printStackTrace(); } } /** * @see java.awt.event.ActionListener#actionPerformed(ActionEvent event) * @param event */ public void actionPerformed(ActionEvent event) { // Check if the event is fired from clearbutton. if (event.getSource() == clearButton) { try { // Get rid of the line polyLine = null; // Refresh the foreground thereby removing any text annotation mapBean.refresh(esriViewDrawPhase.esriViewForeground, null, null); } catch (Exception ex) { System.out.println("Exception in clearText#action performed : " + ex); ex.printStackTrace(); } } // Check if the event is fired from fullExtentButton. if (event.getSource() == fullExtentButton) { try { // Assign map controls extent property to the full extent of all the layers mapBean.getFullExtent(); mapBean.setExtent(mapBean.getFullExtent()); } catch (Exception ex) { System.out.println("Exception in fullExtentButton#actionPerformed : " + ex); } } } /** * Description: Class which extends map control event class IMapControlEvents2Adapter to perform left mouse click * action in onMouseDown and displaying text and shape on the map in onAfterDraw. * * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter */ class MapControlListener extends IMapControlEvents2Adapter { /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseDown(IMapControlEvents2OnMouseDownEvent * theEvent) * @param theEvent */ public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent) { try { // If left mouse button if (theEvent.getButton() == 1) { // Create a point and grab hold of the IPoint inteface Point point = new Point(); // Set point properties point.setX(theEvent.getMapX()); point.setY(theEvent.getMapY()); // If this is the first point of a new line if (polyLine == null) { polyLine = new Polyline(); } // Add point to polyline polyLine.addPoint(point, null, null); // Refresh the foreground thereby removing any text annotation mapBean.refresh(esriViewDrawPhase.esriViewForeground, null, null); } else { // If right or middle mouse button zoom to user defined rectangle IEnvelope envelope = null; // Create an envelope and grab hold of the IEnvelope interface envelope = mapBean.trackRectangle(); // If user dragged a rectangle if (envelope != null) mapBean.setExtent(envelope); } } catch (Exception ex) { System.out.println("Exception in MapControlListener#onMouseDown : " + ex); ex.printStackTrace(); } } /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onAfterDraw(IMapControlEvents2OnMouseDownEvent * theEvent) * @param theEvent */ public void onAfterDraw(IMapControlEvents2OnAfterDrawEvent theEvent) { try { // If foreground refreshed and if a line object for splining text exists // and if there's at least two points in the line if (theEvent.getViewDrawPhase() == esriViewDrawPhase.esriViewForeground && polyLine != null && polyLine.getPointCount() > 1) { // Create a line symbol SimpleLineSymbol lineSymbol = new SimpleLineSymbol(); // Set line symbol properties lineSymbol.setColor(getRGBColor(0, 0, 0)); lineSymbol.setWidth(2.0); // Create a text symbol TextSymbol textSymbol = new TextSymbol(); // Create a font symbol StdFont font = new StdFont(); font.setName("Arial"); font.setBold(true); textSymbol.setFont(font); textSymbol.setSize(16); textSymbol.setColor(getRGBColor(0, 0, 0)); // Create a text path and grab hold of the ITextPath interface BezierTextPath textPath = new BezierTextPath(); // Set the text path of the simple text symbol textSymbol.setTextPathByRef(textPath); // Draw the line object and spline the user text around the line mapBean.drawShape(polyLine, lineSymbol); mapBean.drawText(polyLine, getText(), textSymbol); } } catch (Exception ex) { System.out.println("Exception in MapControlListener#onAfterDraw :" + ex); ex.printStackTrace(); } } } /** * Creates rgb color object * * @param red * @param green * @param blue * @return IRgbColor */ RgbColor getRGBColor(int red, int green, int blue) { RgbColor rgbColor = null; try { // Create rgb color and grab hold of the IRGBColor interface rgbColor = new RgbColor(); rgbColor.setRed(red); rgbColor.setGreen(green); rgbColor.setBlue(blue); rgbColor.setUseWindowsDithering(true); } catch (Exception ex) { System.out.println("Error in getRGBFunction :" + ex); ex.printStackTrace(); } return rgbColor; } /** * Returns the string entered in the textfield. * * @return string value containing text input */ String getText() { return textField.getText(); } /** * Start program execution. */ public static void main(String s[]) { try { EngineInitializer.initializeVisualBeans(); // Set the system look and feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); // Create the drawtext object. DrawText drawText = new DrawText(); drawText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } catch (Exception ex) { ex.printStackTrace(); } } static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); } catch (Exception e){e.printStackTrace();} } }