2013-03-13 4 views
0

안녕하세요 내 기능 (특정 색상으로 형상의 컬렉션) 페인트하려고하지만이 오류가 발생합니다 : 그것은 casted 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)

여기

그가 얻을 어디는 예외입니다 :

paintGeometryCollection((GeometryCollection) selectedFeatures(), 
             graphics, viewport, true, 
             stroke,fillPaint, true, 
             stroke, color); 
0,123,516 :

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(); 

//이 줄

여기가 필요로하는 다른 방법입니다 :

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/), 짧은 실행 가능한, 컴파일 가능한, 그렇지 않으면 대신하는 JViewport의 그림에 대한 JLayer (Java7)를 사용해야 게시 setScrollMode (JViewport.Xxx)를 오버라이드하고 자신의 RepaintManager와 함께 사용하십시오 (여기에 게시 된 코드가 잘린 것이 아니라). – mKorbel

+0

'Whatever.getGraphics()'; 'Paper 프린터 '에 인쇄하는'BufferedImage','File'에 인쇄 할 때,이 방법은 첫 번째 이벤트에서 만료 될 내부 스냅 샷을 만듭니다 – mKorbel

+0

은 SSCCE, short, runnable, compilable을 게시하지 않고이 질문에 답할 수 없습니다. – mKorbel

답변

2

public Collection selectedFeatures() 반환 Colecction을하지만 당신은 GeometryCollection에 캐스팅. 내게 그게 문제 야. 더 나은 도움을

라인 paintGeometryCollection((GeometryCollection) selectedFeatures(),

+0

네 그게 문제입니다,하지만 난 그것을 해결하는 방법을 몰라, 난 그냥 다른 geometryCollection을 반환 기능을 시도 : 공공 GeometryCollection의 selectedGeometry() { \t \t \t \t GeometryCollection 형상; \t \t geometry = (GeometryCollection) search(). getGeometry(); \t \t return geometry; \t \t } \t 그러나 실제로 작동하지 않습니다 –

+1

사실 GeometryCollection과 Collection의 두 클래스가 있으며 서로 다릅니다. 당신은 Geometry를 가져 와서 어떻게 든 GeometryCollection으로 변환해야합니다. GeometryCollection은 표준 클래스가 아니므로이를 수행하는 방법을 모른다. 당신은 전이를해야합니다. – StanislavL

+0

그래,하지만 geometric을 geometryCollection으로 변환하는 법을 모르겠다. 캐스트가 작동하지 않습니다. –

관련 문제