2016-11-25 48 views
-1

Java的我想创建一个多边形onMouseMove()事件多边形。创建使用的鼠标移动事件

这里是我试过:

public DrawingListners(JMapPane mappane) { 
    this._Pane = mappane; 
    this._Pane.addMouseListener(new MapMouseAdapter() { 
     @Override 
     public void onMouseClicked(MapMouseEvent ev) { 
      if (ev.getClickCount() == 2 && _Drawing == Drawings.Polygon) { 
       CreateFeatureCollection(); 
       addPolygonToFC(poly); 

       for (int i = 0; i < nNumbers; i++) { 
        System.out.println(poly.xPoints[i] + ":" + poly.yPoints[i]); 
       } 

       notifyParent.firePropertyChange(CallType.Drawing.toString(), null, null); 
       dragged = false; 
       graphics.dispose(); 
       graphics = null; 
       startPos = null; 
       poly = null; 
       nNumbers = 0; 
      } 
     } 

     @Override 
     public void onMousePressed(MapMouseEvent ev) { 
      if (enabled) { 
       startPos = new Point(ev.getPoint()); 
       if (_Drawing != Drawings.Marker) 
        dragged = true; 
       if (poly == null) { 
        poly = new pMapPolygon(); 
       } 
       poly.addPoint(startPos.x, startPos.y, nNumbers); 
       nNumbers++; 
      } 
     } 

     @Override 
     public void onMouseMoved(MapMouseEvent ev) { 
      if (enabled && poly != null) { 
       ensureGraphics(); 
       if (_Drawing == Drawings.Polygon) { 
        poly.addPoint(ev.getPoint().x, ev.getPoint().y, nNumbers); 
        //graphics.drawPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length); 

        graphics.setColor(Color.blue); 
        graphics.fillPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length); 
        //graphics.drawPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length); 

       } 
      } 
     } 

     @Override 
     public void onMouseDragged(MapMouseEvent ev) { 

     } 

     @Override 
     public void onMouseReleased(MapMouseEvent ev) { 

     } 

     private void ensureGraphics() { 
      if (graphics == null) { 
       graphics = (Graphics2D) _Pane.getGraphics(); 
       graphics.setColor(Color.WHITE); 
       graphics.setXORMode(Color.RED); 
      } 
     } 
    }); 
} 
} 
class pMapPolygon { 
int[] xPoints = new int[500]; 
int[] yPoints = new int[500]; 
int nNumber = 0; 

public pMapPolygon() { 
} 

public void addPoint(int x1, int y1, int number) { 
    // System.out.println(x1 + ":" + y1); 
    if (number == 0) { 
     for (int i = number; i < xPoints.length; i++) { 
      xPoints[i] = x1; 
      yPoints[i] = y1; 
     } 
    } else { 
     xPoints[number] = x1; 
     yPoints[number] = y1; 
    } 

    nNumber = number; 
} 
} 

什么我需要做的,实现这一目标?我究竟做错了什么?

我已经创建,其具有所需要的参数和xPointsyPoints来分添加到存储所有xy的阵列的功能的多边形类pMapPolygon

我已经在onMousePressed()onMouseMoved()中调用了该特定函数addPoint()。但是,当我启动应用程序,也不会产生正确的多边形...

+0

什么宇宙是你?这是什么语言?? – gpasch

+0

欢迎来到Stack Overflow。请提供上下文,例如您正在使用的组件的名称。还要说明发生了什么问题:如果它没有生成正确的多边形,请告诉我们正确的多边形与它生成的多边形之间的区别。或显示截图。请阅读http://stackoverflow.com/help/how-to-ask关于如何提出可以及时准确回答的问题。 – LarsH

回答

0

这需要一段时间,建议在地图控件绘制一个适当的方式,你需要重写你的地图控件的paintComponent方法。

@Override 
protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (drawingLock.tryLock()) { 
      try { 
       Graphics2D g2 = (Graphics2D) g; 
       if (baseImage != null) {       
         g2.drawImage(baseImage, imageOrigin.x, imageOrigin.y, null); 
       } 
       if (this._Drawing != null) { 
         if (this.poly != null && this._Drawing == Drawings.Polygon) {        
          //g.setColor(Color.gray); 
          g2.fillPolygon(this.poly.xPoints, this.poly.yPoints, this.poly.xPoints.length); 
         } 
         if (this.rect != null && this._Drawing == Drawings.Rectangle) { 
          //g.setColor(Color.gray); 
          g2.fillRect(rect.x, rect.y, rect.width, rect.height); 
         } 
         if (this.rect != null && this._Drawing == Drawings.Oval) { 
          //g.setColor(Color.gray); 
          g2.fillOval(rect.x, rect.y, rect.width, rect.height); 
         } 
         if (this.ln != null && this._Drawing == Drawings.Line) { 
          //g.setColor(Color.black); 
          g2.drawLine(ln._x1, ln._y1, ln._x2, ln._y2); 
         } 
       }      
      } finally { 
       drawingLock.unlock(); 
      } 
     } 
}