Possible to set "g" to a P2D PGraphics without crashing?

Hello everyone. I'm trying to create a stack for PGraphics similar to how pushMatrix() and pushStyle() work. So far, it works really well with the default JAVA2D render. The reason I'm doing this is for pure convenience of being able to create classes in which I don't have to supply a reference to a PGraphics, nor have have make calls to drawing methods from a PGraphics object. And for full disclosure, I think what I'm doing is probably bad programming practice.

Here's the proof of concept example: https://gist.github.com/jacobjoaquin/9273597

Right now, if I wanted to draw to a PGraphics object without the stack, I'd have do something like this:

PGraphics pg = createGraphics(500, 500);
pg.beginDraw();
pg.line(0, 0, pg.width, pg.height);
pg.endDraw();

What I want to do, and am so far successful with the JAVA2D render is this:

PGraphics pg = pgs.push();  // Creates a new PGraphics with same dimensions of current "g"
line(0, 0, width, height);    // Writes to pg, without requiring specifying the pg object
pgs.pop();    // Returns to the previous PGraphics

Internally, this works by adding "g" to an ArrayList, creating a new PGraphics pg and setting g to pg. I also set global width and height to pg.width and and pg.height (which I'm sure is not recommended). Having spent a few hours using Processing in this way, I've come to the conclusion that this makes working with layers a breeze, and I can use a large backlog of classes I've created without having to convert them to draw to a PGraphics.

Where this breaks down is when I try to use the P2D or P3D renders. If I do something like this:

PGraphics temp = g;
PGraphics pg = createGraphics(100, 100, P2D);
g = pg;  
g.beginDraw();  // Breaks here
line(width, random(height), 0, height);
g.endDraw();
g = temp;
image(pg, 0, 0);

In a nutshell, Processing has an issue with setting g = createGraphics(100, 100, P2D). Personally I'm surprised that it works with a JAVA2D PGraphics. That said, I'd really like to get this working if possible. Does anyone know how to overcome this? Or possibly any strategies on how to figure this out?

Thanks.

Tagged:

Answers

  • edited February 2014

    In order to use P2D, P3D, OPENGL as argument in createGraphics(), we gotta have a size() using 1 of them as well!

    // forum.processing.org/two/discussion/3361/
    // possible-to-set-g-to-a-p2d-pgraphics-without-crashing
    
    size(100, 100, P2D);
    noLoop();
    
    PGraphics temp = g;
    PGraphics pg = g = createGraphics(width, height, P2D);
    
    g.beginDraw();
    line(width, random(height), 0, height);
    g.endDraw();
    
    g = temp;
    background(pg);
    
  • Thanks for your response. I'm still having issues assigning "g" to a P2D PGraphics object. There is no issue with JAVA2D, even when the primary display is set to P2D. This is the code I'm working off of:

    void setup() {
      size(100, 100, P2D);
      noLoop();
    
      PGraphics temp = g;
      PGraphics pg = g = createGraphics(width, height, P2D);  // Breaks
    //  PGraphics pg = g = createGraphics(width, height);       // Works
    
      g.beginDraw();
      line(width, random(height), 0, height);
      g.endDraw();
    
      g = temp;
      image(pg, 0, 0);
    }
    
  • edited March 2014 Answer ✓

    That's not much diff. than my example version. And both worked alright to me! %%-
    I'm using Processing 2.0.2 w/ 64-bit OpenJDK 7.51@ Lubuntu 13.04 w/ GeForce 9800GT driver v331.20.

  • Yep, this looks like a versioning issue. The code works in 1.5.1 but not 2.1.1. I'll dig deeper and hopefully pinpoint exactly what the issue is. Thanks for your help.

Sign In or Register to comment.