Exception on drawing Path2D

Hi All

I wonder if anyone can help me with an exception I’m getting using Path2D. I’m generating a line for a river like so:

  
        float px=0;
        float py = dimM/2;        
                
        Fbm fbm = new Fbm(2.0f, 1.5f, 8f);
        float s = 0.0005f;
        float offset = rnd.nextFloat() * 999f;
        
        river = new Path2D.Double();       
        river.moveTo(px,py);  
        
        for(int i=0; i<dimM; i++){
            px = (float)i;
            py += fbm.fBm(offset + px*s, py*s) * 5;
            river.lineTo(px,py);    
        }

and rendering with:


 public void paint(Graphics g1){
            Graphics2D g = (Graphics2D) g1;
            g.setColor(Color.BLUE);            
            g.draw(river);
}

The exception I’m getting is:


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2280
	at java.awt.geom.AffineTransform.transform(AffineTransform.java:3405)
	at java.awt.geom.Path2D$Double$TxIterator.currentSegment(Path2D.java:1686)
	at sun.dc.DuctusRenderingEngine.getAATileGenerator(DuctusRenderingEngine.java:442)
	at sun.java2d.pipe.AAShapePipe.renderPath(AAShapePipe.java:152)
	at sun.java2d.pipe.AAShapePipe.draw(AAShapePipe.java:64)
	at sun.java2d.pipe.PixelToParallelogramConverter.draw(PixelToParallelogramConverter.java:148)
	at sun.java2d.pipe.PixelToParallelogramConverter.draw(PixelToParallelogramConverter.java:148)
	at sun.java2d.SunGraphics2D.draw(SunGraphics2D.java:2461)
	at city.City$CityPanel.paint(City.java:163)

        etc....

Please. post more info about City class.

I guess it is multithreading issue.

Modify your code in the following way:
For adding the path segments


synchronized (river){
  river.lineTo();
}

For drawing


synchronized (river){
   g2.draw(river);
}

Please, let me know if it works.

I had a similar problem, and synchronized fixed it. Thank you very much.