【问题标题】:Create a Polygon using Java on Mouse move Event在鼠标移动事件上使用 Java 创建多边形
【发布时间】:2016-11-25 13:52:54
【问题描述】:

我想创建一个多边形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;
}
}

我需要做什么才能实现这一目标?我做错了什么?

我创建了一个多边形类pMapPolygon,它具有所需的参数xPointsyPoints,以及一个向存储所有xy 的数组添加点的函数。

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

【问题讨论】:

  • 你在哪个宇宙??这是什么语言??
  • 欢迎来到 Stack Overflow。请提供上下文,例如您正在使用的组件的名称。还要描述发生了什么问题:如果它没有产生正确的多边形,请告诉我们正确的多边形和它产生的多边形之间的区别。或显示屏幕截图。请阅读stackoverflow.com/help/how-to-ask,了解如何提出可以及时准确地回答的问题。

标签: java swing graphics maps


【解决方案1】:

建议在地图控件中绘制正确的方法需要一段时间,您需要覆盖地图控件的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();
            }
       }
 }

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多