Loading...
Logo
Processing Forum
I'm using the Processing library in Netbeans and I have a PGraphics object, in which I create vertex shapes. This works fine in JAVA2D ( image here) but not in P2D. I have it working, so that's all good - but was wondering what the problem is with P2D?

I'm drawing polygons, playing with this library for loading shapefiles (which I don't see in the library list, btw...)

There's a method I pass the PGraphics to, in an object where the polygon data sits. I'm doing it this way so I can draw once, then re-use the image. Anything else is waaay too slow. Method below. In JAVA2D, "do we get here" gets to screen, in P2D not, so it seems to be closing the shape (pg.endShape) that fails: it just hangs.

Any thoughts?

While I'm writing: another shapefile loader in the library (new, MapThing) links only to docs, not to the download zip. I found the zip here.

cheers...

Copy code
  1. public void drawShapeToPGraphics(PGraphics pg) {
           
            pg.fill(100);
            pg.stroke(255);
           
            pg.beginShape();
           
            for (double[] pts : points) {
               
                pg.vertex((float) pts[0] * 1000, 1000 - ((float) pts[1] * 1000));

            }

            pg.endShape(pg.CLOSE);       
            System.out.println("do we get here?");
           }//end method

Replies(5)

Try adding pg.beginDraw() at the beginning and pg.endDraw at the end.
Yes, I've got beginDraw and endDraw for the PGraphics outside that method, on each side of a foreach loop for each object I'm drawing to the PGraphics...
Ok. Vertices do work with a P2D Pgraphics, see the example. So it's something in your code that causes the problem. You could add some more println's. If it hangs it could be just as well hanging somewhere else.
Copy code
  1. PGraphics pg;
  2.  
  3. void setup() {
  4.   size(500, 500, P2D);
  5.   background(255);
  6.   pg = createGraphics(width/2, height/2, P2D);
  7.   pg.beginDraw();
  8.   pg.background(0);
  9.   pg.fill(100);
  10.   pg.stroke(255);
  11.   pg.beginShape();
  12.   pg.vertex(pg.width/2, 0);
  13.   pg.vertex(pg.width, pg.height/2);
  14.   pg.vertex(pg.width/2, pg.height);
  15.   pg.vertex(0, pg.height/2);
  16.   pg.endShape(pg.CLOSE);
  17.   pg.endDraw();
  18.   image(pg, width/4, width/4);
  19. }
After some more testing: both engines do draw succesfully, you're right - it's just that P2D is drawing shapes much slower than JAVA2D - so slowly it appeared stuck. I'm creating shapes from ShapeFile zone polygons and filling them, with some having tens of thousands of vertices. JAVA2D appears to be able to manage that very quickly compared to P2D.
If you have that many vertices it may be wise to switch to OPENGL.

For example by using the GLGraphics library which support GLGraphicsOffscreen (aka PGraphics for OPENGL).

Or the Processing 2.0 alpha's (warning, it ain't called alpha for nothing) with it's retained PShape.