Accessing the contextual PGraphics explicitly when using beginRecord()

I am passing this.g to some separate Java code, and things are working just fine with the data displayed to the screen, but when I try to copy to PDF using beginRecord() and endRecord() I don’t see any of the output that is generated via calls to this.g.

How do I fix this?

For example:

import processing.pdf.PGraphicsPDF;

void settings() {
  size(200, 200, P2D);
}

void setup() {  
  noLoop();
}

void draw() {
  beginRecord(PDF, "test.pdf");
  line(0, 0, 100, 100);
  new Test().line(this.g);
  save("test.png");
  endRecord();
}

where Test.java contains:

import processing.core.PGraphics;

public class Test {

    public void line(PGraphics g) {
        g.line(200, 0, 100, 100);
    }
    
}

will show TWO lines on the display and in the PNG file, but only ONE in the PDF (the second line, generated in the Java code, is missing).

Thanks!

Just to add to this… the call to beginRecord() returns a PGraphics instance. If I use that, instead of this.g, in the call to the Java code, then I see the reverse: TWO lines are present in the PDF, but only one on the screen and in the PNG! Is there any way to have both lines present in all the different outputs (without repeating the calls twice, obviously)?

(Fundamentally, I guess I am asking how the recording process is implemented. I can’t find it documented anywhere).

Hello @andrewcooke,

It works with this:

  beginRaw(PDF, "test.pdf");
  //...
  endRaw(

There is a note about P2D here in the PDF Files from 3D Geometry (With Screen Display) section:

The source for the PDF library is here to explore:

:)

just to be clear for anyone else, because it took me a minute and i tested it - just replace “Record” with “Raw” in the example above and things work fine.

THANKS!

edit: well, when used on my actual plot, it’s taking forever and a lot of cpu, but i guess it’s working…

1 Like