package demo1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicBorders;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.CircleElement;
import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IEnumElement;
import com.esri.arcgis.carto.IFillShapeElement;
import com.esri.arcgis.carto.IGraphicsContainer;
import com.esri.arcgis.carto.ILineElement;
import com.esri.arcgis.carto.LineElement;
import com.esri.arcgis.carto.PolygonElement;
import com.esri.arcgis.carto.RectangleElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.IMapControlEvents2Adapter;
import com.esri.arcgis.controls.IMapControlEvents2OnDoubleClickEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseUpEvent;
import com.esri.arcgis.controls.MapControl;
import com.esri.arcgis.display.IColor;
import com.esri.arcgis.display.IDisplayFeedback;
import com.esri.arcgis.display.ILineMovePointFeedback;
import com.esri.arcgis.display.IMoveGeometryFeedback;
import com.esri.arcgis.display.INewBezierCurveFeedback;
import com.esri.arcgis.display.INewCircleFeedback;
import com.esri.arcgis.display.INewEnvelopeFeedback;
import com.esri.arcgis.display.INewLineFeedback;
import com.esri.arcgis.display.INewPolygonFeedback;
import com.esri.arcgis.display.IPolygonMovePointFeedback;
import com.esri.arcgis.display.IScreenDisplay;
import com.esri.arcgis.display.ISimpleFillSymbol;
import com.esri.arcgis.display.ISimpleLineSymbol;
import com.esri.arcgis.display.IStretchLineFeedback;
import com.esri.arcgis.display.LineMovePointFeedback;
import com.esri.arcgis.display.MoveGeometryFeedback;
import com.esri.arcgis.display.NewBezierCurveFeedback;
import com.esri.arcgis.display.NewCircleFeedback;
import com.esri.arcgis.display.NewEnvelopeFeedback;
import com.esri.arcgis.display.NewLineFeedback;
import com.esri.arcgis.display.NewPolygonFeedback;
import com.esri.arcgis.display.PolygonMovePointFeedback;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.display.StretchLineFeedback;
import com.esri.arcgis.display.esriEnvelopeConstraints;
import com.esri.arcgis.display.esriSimpleFillStyle;
import com.esri.arcgis.display.esriSimpleLineStyle;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IHitTest;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPolygon;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.ISegment;
import com.esri.arcgis.geometry.ISegmentCollection;
import com.esri.arcgis.geometry.ITransform2D;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geometry.esriGeometryHitPartType;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.interop.AutomationException;
public class Operations extends JFrame implements ActionListener{
private JPanel buttonPanel = null;
private JPanel mainPanel = null;
private JPanel statusPanel = null;
private final String[] buttonText = {"新建多义线", "新建圆", "新建矩形", "新建多边形", "新建Bezieri曲线", "拉伸多义线", "移动多义线节点", "移动多边形节点", "移动几何对象(多个)"};
private JButton[] buttons = new JButton[buttonText.length];
private JLabel nowStatus = new JLabel();
private JLabel statusLocation = new JLabel();
private MapBean mapBean;
private IDisplayFeedback pDisplayFeedback;
private IGraphicsContainer pGraphicContainer;
private IActiveView pActiveView;
private IScreenDisplay pScreenDisplay;
private IElement hitElement;//鼠标点击的element
private IEnumElement pEnumElement;//element集合,移动几何对象中需要用到
private IPoint moveGeometryStartPoint;//移动几何对象时开始的位置
public Operations() {
super("绘图");
buildFrame();
setSize(1000, 600);
Dimension dimension = getToolkit().getScreenSize();
setLocation((dimension.width-getWidth())/2, (dimension.height-getHeight())/2);
setVisible(true);
setDefaultCloseOperation(Operations.EXIT_ON_CLOSE);
}
public void buildFrame() {
buttonPanel = new JPanel();
mainPanel = new JPanel();
statusPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
FlowLayout fl = new FlowLayout();
fl.setAlignment(FlowLayout.LEFT);
statusPanel.setLayout(fl);
nowStatus.setText(buttonText[0]);
statusLocation.setText("x:0 y:0");
statusPanel.add(nowStatus);
statusPanel.add(Box.createHorizontalStrut(30));
statusPanel.add(statusLocation);
statusPanel.setBorder(BorderFactory.createLineBorder(Color.red));
// statusPanel.add(nowStatus);
// statusPanel.add(Box.createHorizontalStrut(30));
// statusPanel.add(statusLocation);
for(int i=0; i
buttons[i].setToolTipText(buttonText[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
mapBean = new MapBean();
mainPanel.add(buttonPanel, BorderLayout.NORTH);
mainPanel.add(mapBean, BorderLayout.CENTER);
mainPanel.add(statusPanel,BorderLayout.SOUTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//重要
try {
mapBean.addIMapControlEvents2Listener(new MapControlListener());
pScreenDisplay = mapBean.getActiveView().getScreenDisplay();
pGraphicContainer = (IGraphicsContainer)mapBean.getActiveView().getFocusMap();
pActiveView = mapBean.getActiveView();
}
catch (IOException e) {
e.printStackTrace();
}
getContentPane().add(mainPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent event) {
Object object = event.getSource();
if(object instanceof JButton)
nowStatus.setText(((JButton)object).getText());
}
private void addElement(IGeometry pGeometry)
{
try
{
IElement pElement = null;
ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
pSimpleLineSymbol.setWidth(3);
RgbColor rc = new RgbColor();
rc.setRed(255);
rc.setBlue(255);
pSimpleLineSymbol.setColor(rc);
pSimpleLineSymbol.setStyle(esriSimpleLineStyle.esriSLSSolid);
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid);
pSimpleFillSymbol.setOutline(pSimpleLineSymbol);
if (pGeometry.getGeometryType() == esriGeometryType.esriGeometryPolyline)//多义线
{
ILineElement pLineElement = new LineElement();
pLineElement.setSymbol(pSimpleLineSymbol);
pElement = (IElement)pLineElement;
pElement.setGeometry(pGeometry);
}
else if (pGeometry.getGeometryType() == esriGeometryType.esriGeometryCircularArc)//圆
{
ISegmentCollection pSegmentCollection;
pSegmentCollection = new Polygon();
//object Missing = Type.Missing;//注意
pSegmentCollection.addSegment((ISegment)pGeometry, null, null);//addSegment( as , ref Missing, ref Missing);//后两个参数必须是这样,帮助说的,为什么??
pElement = new CircleElement();
pElement.setGeometry((IGeometry)pSegmentCollection);
IFillShapeElement pFillShapeElement = (IFillShapeElement)pElement;
pFillShapeElement.setSymbol(pSimpleFillSymbol);
}
else if (pGeometry.getGeometryType() == esriGeometryType.esriGeometryEnvelope)//矩形
{
pElement = new RectangleElement();
pElement.setGeometry(pGeometry);
IFillShapeElement pFillShapeElement = (IFillShapeElement)pElement;
pFillShapeElement.setSymbol(pSimpleFillSymbol);
}
else if (pGeometry.getGeometryType() == esriGeometryType.esriGeometryPolygon)//多边形
{
pElement = new PolygonElement();
pElement.setGeometry(pGeometry);
IFillShapeElement pFillShapeElement = (IFillShapeElement)pElement;
pFillShapeElement.setSymbol(pSimpleFillSymbol);
}
else if (pGeometry.getGeometryType() == esriGeometryType.esriGeometryBezier3Curve)//Bezier曲线
{
pElement = new LineElement();
pElement.setGeometry(pGeometry);
ILineElement pLineElement = (ILineElement)pElement;
pLineElement.setSymbol(pSimpleLineSymbol);
}
this.pGraphicContainer.addElement(pElement, 0);
this.mapBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private IElement getElement(IPoint pPoint,int geometryType) throws AutomationException, IOException
{
IEnumElement pEnumElement = this.pGraphicContainer.locateElements(pPoint, mapBean.getActiveView().getExtent().getWidth()/100);
if (pEnumElement != null)
{
pEnumElement.reset();
IElement pElement;
for (pElement=pEnumElement.next(); pElement!= null;pElement=pEnumElement.next())
{
if (pElement.getGeometry().getGeometryType() == geometryType)
{
this.hitElement = pElement;
return pElement;
}
}
return null;
}
return null;
}
class MapControlListener extends IMapControlEvents2Adapter {
public void onDoubleClick(IMapControlEvents2OnDoubleClickEvent e) throws IOException, AutomationException {
if (pDisplayFeedback != null)
{
IGeometry pGeometry = null;
if (nowStatus.getText().equals(buttonText[0]))//新建多义线
{
pGeometry = ((INewLineFeedback)pDisplayFeedback).stop();
}
else if (nowStatus.getText().equals(buttonText[3]))//新建多边形
{
pGeometry = ((INewPolygonFeedback)pDisplayFeedback).stop();
}
else if (nowStatus.getText().equals(buttonText[4]))//新建Bezier曲线
{
pGeometry = ((INewBezierCurveFeedback)pDisplayFeedback).stop();
}
pDisplayFeedback = null;
addElement(pGeometry);
}
}
public void onMouseDown(IMapControlEvents2OnMouseDownEvent e) throws IOException, AutomationException {
try
{
if (e.getButton() == 1)//按左键的话
{
IPoint pPoint = new Point();//在鼠标点击的位置生成一个点
pPoint.putCoords(e.getMapX(), e.getMapY());
if (nowStatus.getText().equals(buttonText[0]))//新建多义线
{
if (pDisplayFeedback == null)//如果是第一次点击,就建立第一个节点
{
pDisplayFeedback = new NewLineFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((INewLineFeedback)pDisplayFeedback).start(pPoint);
}
else//如果不是第一次点击,就添加节点
{
((INewLineFeedback)pDisplayFeedback).addPoint(pPoint);
}
}
else if (nowStatus.getText().equals(buttonText[6]))//移动多义线节点
{
IElement pElement = getElement(pPoint, esriGeometryType.esriGeometryPolyline);
if (pElement != null)
{
IPolyline pPolyline = (IPolyline)pElement.getGeometry();
IHitTest pHitTest = (IHitTest)pPolyline;
IPoint hitPoint = new Point();
double[] distance = new double[1];
boolean[] isOnRightSide = new boolean[1];
int[] hitPartIndex = new int[1];
int[] hitSegmentIndex = new int[1];
//这个和C#的不同
boolean isHit = pHitTest.hitTest(pPoint,
mapBean.getActiveView().getExtent().getWidth(),
esriGeometryHitPartType.esriGeometryPartVertex,
hitPoint,
distance,
hitPartIndex,
hitSegmentIndex,
isOnRightSide);
if (isHit)
{
pDisplayFeedback = new LineMovePointFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((ILineMovePointFeedback)pDisplayFeedback).start(pPolyline, hitSegmentIndex[0], pPoint);
}
}
}
else if (nowStatus.getText().equals(buttonText[1]))//画圆
{
pDisplayFeedback = new NewCircleFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((INewCircleFeedback)pDisplayFeedback).start(pPoint);
}
else if (nowStatus.getText().equals(buttonText[2]))//画矩形
{
pDisplayFeedback = new NewEnvelopeFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((INewEnvelopeFeedback)pDisplayFeedback).setConstraint(esriEnvelopeConstraints.esriEnvelopeConstraintsNone);
((INewEnvelopeFeedback)pDisplayFeedback).start(pPoint);
}
else if (nowStatus.getText().equals(buttonText[3]))//画多边形
{
if (pDisplayFeedback == null)//如果是第一次点击,就建立第一个节点
{
pDisplayFeedback = new NewPolygonFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((INewPolygonFeedback)pDisplayFeedback).start(pPoint);
}
else//如果不是第一次点击,就添加节点
{
((INewPolygonFeedback)pDisplayFeedback).addPoint(pPoint);
}
}
else if (nowStatus.getText().equals(buttonText[7]))//移动多边形节点
{
IElement pElement = getElement(pPoint, esriGeometryType.esriGeometryPolygon);
if (pElement != null)
{
IPolygon pPolygon = (IPolygon)pElement.getGeometry();
IHitTest pHitTest = (IHitTest)pPolygon;
IPoint hitPoint = new Point();
double[] distance = new double[1];
boolean[] isOnRightSide = new boolean[1];
int[] hitPartIndex = new int[1];
int[] hitSegmentIndex = new int[1];
//这个和C#的不同
boolean isHit = pHitTest.hitTest(pPoint,
mapBean.getActiveView().getExtent().getWidth()/100,
esriGeometryHitPartType.esriGeometryPartVertex,
hitPoint,
distance,
hitPartIndex,
hitSegmentIndex,
isOnRightSide);
if (isHit)
{
pDisplayFeedback = new PolygonMovePointFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((IPolygonMovePointFeedback)pDisplayFeedback).start(pPolygon, hitSegmentIndex[0], pPoint);
}
}
}
else if (nowStatus.getText().equals(buttonText[4]))//新建Bezier曲线
{
if (pDisplayFeedback == null)//如果是第一次点击
{
pDisplayFeedback = new NewBezierCurveFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((INewBezierCurveFeedback)pDisplayFeedback).start(pPoint);
}
else//如果不是第一次点击
{
((INewBezierCurveFeedback)pDisplayFeedback).addPoint(pPoint);
}
}
else if (nowStatus.getText().equals(buttonText[8]))//移动几何对象
{
pEnumElement = pGraphicContainer.locateElements(pPoint, pActiveView.getExtent().getWidth()/100);
if (pEnumElement != null)
{
pDisplayFeedback = new MoveGeometryFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
IElement pElement;
pEnumElement.reset();
//需要逐个逐个添加Geometry
for (pElement = pEnumElement.next(); pElement != null; pElement = pEnumElement.next())
{
((IMoveGeometryFeedback)pDisplayFeedback).addGeometry(pElement.getGeometry());
}
((IMoveGeometryFeedback)pDisplayFeedback).start(pPoint);
moveGeometryStartPoint = pPoint;
}
}
else if (nowStatus.getText().equals(buttonText[5]))//拉伸多义线
{
IElement pElement = getElement(pPoint, esriGeometryType.esriGeometryPolyline);
if (pElement != null)
{
IPolyline pPolyline = (IPolyline)(pElement.getGeometry());
pDisplayFeedback = new StretchLineFeedback();
pDisplayFeedback.setDisplayByRef(pScreenDisplay);
((IStretchLineFeedback)pDisplayFeedback).setAnchor(pPolyline.getFromPoint());
((IStretchLineFeedback)pDisplayFeedback).start(pPolyline, pPoint);
}
}
}
else if (e.getButton() == 2)//按右键的就把画面清空
{
if (pDisplayFeedback == null)
{
pGraphicContainer.deleteAllElements();
// mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
mapBean.getActiveView().refresh();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
@Override
public void onMouseUp(IMapControlEvents2OnMouseUpEvent e) throws IOException, AutomationException {
if (pDisplayFeedback != null)
{
if (nowStatus.getText().equals(buttonText[6]))//移动多义线节点
{
IGeometry pGeometry = ((ILineMovePointFeedback)pDisplayFeedback).stop();
if (pGeometry != null)
{
hitElement.setGeometry(pGeometry);
pGraphicContainer.updateElement(hitElement);
}
hitElement = null;
pDisplayFeedback = null;
mapBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else if (nowStatus.getText().equals(buttonText[1]))//画圆
{
IGeometry pGeometry = ((INewCircleFeedback)pDisplayFeedback).stop();
addElement(pGeometry);
pDisplayFeedback = null;
}
else if (nowStatus.getText().equals(buttonText[2]))//画矩形
{
IGeometry pGeometry = ((INewEnvelopeFeedback)pDisplayFeedback).stop();
addElement(pGeometry);
pDisplayFeedback = null;
}
else if (nowStatus.getText().equals(buttonText[7]))//移动多边形节点
{
IGeometry pGeometry = ((IPolygonMovePointFeedback)pDisplayFeedback).stop();
if (pGeometry != null)
{
hitElement.setGeometry(pGeometry);
pGraphicContainer.updateElement(hitElement);
}
hitElement = null;
pDisplayFeedback = null;
mapBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else if (nowStatus.getText().equals(buttonText[8]))//移动几何对象
{
if (pEnumElement != null)
{
pEnumElement.reset();
IElement pElement;
ITransform2D pTransform2D;
//需要用ITransform2D接口逐个逐个的移动element
for (pElement = pEnumElement.next(); pElement != null; pElement = pEnumElement.next())
{
pTransform2D = (ITransform2D)pElement;
pTransform2D.move(e.getMapX() - moveGeometryStartPoint.getX(), e.getMapY() - moveGeometryStartPoint.getY());
pGraphicContainer.updateElement(pElement);
}
moveGeometryStartPoint = null;
pEnumElement = null;
pDisplayFeedback = null;
mapBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
else if (nowStatus.getText().equals(buttonText[5]))//拉伸多义线
{
if (hitElement != null)
{
IGeometry pGeometry = ((IStretchLineFeedback)pDisplayFeedback).stop();
hitElement.setGeometry(pGeometry);
pGraphicContainer.updateElement(hitElement);
hitElement = null;
pDisplayFeedback = null;
mapBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
}
}
public void onMouseMove(IMapControlEvents2OnMouseMoveEvent e) throws IOException, AutomationException {
IPoint pPoint = new Point();
pPoint.putCoords(e.getMapX(), e.getMapY());
statusLocation.setText("x:" + e.getMapX() + " y:" + e.getMapY());
if (pDisplayFeedback != null)
{
pDisplayFeedback.moveTo(pPoint);
}
}
}
public static void main(String s[]) {
try {
EngineInitializer.initializeVisualBeans();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
Operations geoEvents = new Operations();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
这个demo需要ArcGis环境,没有环境则不能运行。