【问题标题】:Paint my collection of geometry feature?绘制我的几何特征集合?
【发布时间】:2013-03-01 07:09:17
【问题描述】:

您好,我正在尝试绘制我的特征(具有特定颜色的几何集合),但我收到此错误: 这是要转换的arraylist的问题,但我不知道如何解决它 LZ帮帮我 提前致谢

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 

java.util.ArrayList 不能转换为 com.vividsolutions.jts.geom.GeometryCollection 在 com.vividsolutions.jump.workbench.ui.plugin.specific.SearchPropertiesPlugin$3.actionPerformed(SearchPropertiesPlugin.java:205) 在 javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 在 javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 在 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 在 java.awt.Component.processMouseEvent(Component.java:6297) 在 javax.swing.JComponent.processMouseEvent(JComponent.java:3275) 在 java.awt.Component.processEvent(Component.java:6062) 在 java.awt.Container.processEvent(Container.java:2039) 在 java.awt.Component.dispatchEventImpl(Component.java:4660) 在 java.awt.Container.dispatchEventImpl(Container.java:2097) 在 java.awt.Component.dispatchEvent(Component.java:4488) 在 java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575) 在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236) 在 java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166) 在 java.awt.Container.dispatchEventImpl(Container.java:2083) 在 java.awt.Window.dispatchEventImpl(Window.java:2489) 在 java.awt.Component.dispatchEvent(Component.java:4488) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668) 在 java.awt.EventQueue.access$400(EventQueue.java:81) 在 java.awt.EventQueue$2.run(EventQueue.java:627) 在 java.awt.EventQueue$2.run(EventQueue.java:625) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 在 java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 在 java.awt.EventQueue$3.run(EventQueue.java:641) 在 java.awt.EventQueue$3.run(EventQueue.java:639) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:638) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

这是他得到的例外:

Viewport viewport = new Viewport(context.getLayerViewPanel());
Paint fillPaint = null;
Color color = Color.yellow;
Stroke stroke =new BasicStroke(5, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
final Graphics2D graphics = (Graphics2D) context.getLayerViewPanel().getGraphics();

//在这一行:

paintGeometryCollection((GeometryCollection) selectedFeatures(),
                                        graphics, viewport, true,
                                        stroke,fillPaint, true,
                                        stroke, color);

这是它需要的其他方法:

public Collection selectedFeatures() {
    ArrayList selectedFeatures = new ArrayList();
    for(BasicFeature basicFeature : TitreList) {
    selectedFeatures.add(search().getGeometry());
    // search() is a BasicFeature
    }
    return selectedFeatures;
}   


    private static void paintGeometryCollection(GeometryCollection collection,
        Graphics2D g, Viewport viewport, boolean renderingFill,
        Stroke fillStroke, Paint fillPaint, boolean renderingLine,
        Stroke lineStroke, Color lineColor)
        throws NoninvertibleTransformException {
        //For GeometryCollections, render each element separately. Otherwise,
        //for example, if you pass in a GeometryCollection containing a ring and a
        // disk, you cannot render them as such: if you use Graphics.fill, you'll get
        //two disks, and if you use Graphics.draw, you'll get two rings. [Jon Aquino]
        for (int i = 0; i < collection.getNumGeometries(); i++) {
            paint(collection.getGeometryN(i), g, viewport, renderingFill,
                fillStroke, fillPaint, renderingLine, lineStroke, lineColor);
        }
    }


public static void paint(Geometry geometry, Graphics2D g,
        Viewport viewport, boolean renderingFill, Stroke fillStroke,
        Paint fillPaint, boolean renderingLine, Stroke lineStroke,
        Color lineColor) throws NoninvertibleTransformException {
        if (geometry instanceof GeometryCollection) {
            paintGeometryCollection((GeometryCollection) geometry, g, viewport,
                renderingFill, fillStroke, fillPaint, renderingLine,
                lineStroke, lineColor);

            return;
        }

        Shape shape = toShape(geometry, viewport);
        if (!(shape instanceof GeneralPath) && renderingFill) {
            g.setStroke(fillStroke);
            g.setPaint(fillPaint);
            g.fill(shape);
        }
        if (renderingLine) {
            g.setStroke(lineStroke);
            g.setColor(lineColor);
            g.draw(shape);
        }
    }

private static Shape toShape(Geometry geometry, Viewport viewport)
throws NoninvertibleTransformException {
//At high magnifications, Java rendering can be sped up by clipping
//the Geometry to only that portion visible inside the viewport.
//Hence the code below. [Jon Aquino]
Envelope bufferedEnvelope = EnvelopeUtil.bufferByFraction(viewport.getEnvelopeInModelCoordinates(),
        0.05);
Geometry actualGeometry = geometry;
Envelope geomEnv = actualGeometry.getEnvelopeInternal();
if (! bufferedEnvelope.contains(geomEnv)) {
  /**
   * MD - letting Java2D do more clipping actually seems to be slower!
   * So don't use following "optimization"
   */
  //if (isRatioLarge(bufferedEnvelope, geomEnv, 2)) {
    if (!((geometry instanceof LineString) || (geometry instanceof MultiLineString)))
        actualGeometry = clipGeometry(geometry, bufferedEnvelope);
    //System.out.println("cl");
  //}
}
return viewport.getJava2DConverter().toShape(actualGeometry);
   }

private static Geometry clipGeometry(Geometry geom, Envelope env)
{
  try {
      Geometry clipGeom = EnvelopeUtil.toGeometry(env)
                                   .intersection(geom);
      return clipGeom;
  } catch (Exception e) {
      //Can get a TopologyException if the Geometry is invalid. Eat it. [Jon Aquino]
      //Can get an AssertionFailedException (unable to assign hole to a shell)
      //at high magnifications. Eat it. [Jon Aquino]

      //Alvaro Zabala reports that we can get here with an
      //IllegalArgumentException (points must form a closed linestring)
      //for bad geometries. Eat it. [Jon Aquino]
  }
  return geom;
}

【问题讨论】:

  • 为了更好的帮助尽快发布SSCCE,简短,可运行,可编译,使用JLayer(Java7)而不是JViewport进行绘画,否则必须覆盖setScrollMode(JViewport.Xxx)并与自己一起使用RepaintManager(确保这些方法不是此处发布的代码片段)
  • 使用Whatever.getGraphics();仅用于打印到BufferedImageFile,打印到paper printer,此方法创建将在第一个事件时过期的内部快照
  • 如果不发布 SSCCE、简短、可运行、可编译,则无法回答此问题
  • @mKorbel oki,但两者有什么区别? (Jlayer 和 JViewport)。因为现在正在研究 OpenJump 的代码源,他们正在使用 JViewport。
  • 从上个千年跳到现在:-)

标签: java swing paint computational-geometry jviewport


【解决方案1】:

public Collection selectedFeatures() 返回Colecction,但您将其转换为GeometryCollection。对我来说,这就是问题所在。

一行paintGeometryCollection((GeometryCollection) selectedFeatures(),

【讨论】:

  • 是的,这就是问题所在,但我不知道如何解决它,我只是尝试使用另一个返回 geometryCollection 的函数: public GeometryCollection selectedGeometry() { GeometryCollection geometry;几何 = (GeometryCollection) search().getGeometry();返回几何; } 但它也不起作用
  • 实际上有 2 个类 GeometryCollection 和 Collection,它们是不同的。你应该得到 Geometry 并以某种方式转换为 GeometryCollection。 GeometryCollection 不是标准类,所以我们不知道该怎么做。您必须进行转换。
  • 是的,但我不知道如何将几何转换为几何集合。演员表不起作用:s
猜你喜欢
  • 1970-01-01
  • 2017-11-14
  • 2020-05-09
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2020-01-16
相关资源
最近更新 更多