2013-03-13 40 views
0

你好我想绘制我的特征(具有特定颜色的几何体集合),但我得到这个错误: 这是一个问题与arraylist被铸造,但我不怎么修复它 PLZ帮助我 谢谢推进绘制我的几何特征集合?

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

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

为更好地帮助更快张贴[SSCCE(http://sscce.org/),短,可运行,编译,使用JLayer(Java7)的画,而不是JViewport的,否则必须覆盖setScrollMode(JViewport.Xxx)并与自己的RepaintManager一起使用(确保这些方法不是在这里发布的代码片断) – mKorbel 2013-03-13 11:28:14

+0

使用'Whatever.getGraphics()';仅用于打印到'BufferedImage','File',打印到'纸打印机',这种方法创建内部快照,将在第一个事件 – mKorbel 2013-03-13 11:33:37

+0

到期,无需发布SSCCE,可短期,可运行,可编译 – mKorbel 2013-03-13 11:34:04

回答

2

public Collection selectedFeatures()回报Colecction,但你把它转换为GeometryCollection。对我而言,这是问题所在。

线paintGeometryCollection((GeometryCollection) selectedFeatures(),

+0

是这就是问题所在,但我不知道如何解决它,我只是想与另一个函数,返回GEOMETRYCOLLECTION:公众的GeometryCollection selectedGeometry(){ \t \t \t \t GeometryCollection的几何形状; ()GeometryCollection)search()。getGeometry(); \t \t返回几何; \t \t } \t但它既不工作也不工作 – 2013-03-13 12:04:46

+1

实际上有两个类GeometryCollection和Collection,它们是不同的。你应该得到几何,并以某种方式转换为GeometryCollection。 GeometryCollection不是标准类,所以我们不知道如何做到这一点。你必须做transfromation。 – StanislavL 2013-03-13 13:44:05

+0

是的,但我不知道如何将geometry转换为geometryCollection。演员表不起作用:s – 2013-03-13 14:48:26

相关问题