Exploring PApplet and PGraphics, trying to create headless graphics

This is 1 of 2 posts. In this one I will describe my attempt to create a standalone PGraphics to a file without a PApplet. I am interested in simplifying code to generate files, and in interoperating with Swing. I am willing to add classes to the hierarchy to do this without interfering with anyone's existing code or how it works.

The problem is that PApplet and PGraphics are extremely convoluted, so any help in trying to unravel would be appreciated.

First of all, I tried to create a PGraphics writing an image without opening a PApplet.

PGraphics g = new PGraphics();
g.setPrimary(false);
g.setSize(320,200);
g.beginDraw(); // seems unnecessary
g.background(0xFF0000);
g.stroke(0xFFFFFF);
g.line(0,0, 300,300);
g.rect(100, 200, 100, 50);
g.endDraw();
String path = System.getProperty("user.dir");
g.save(path + "/test.jpg");

This generates a jpg but it is black, nothing draws. debug shows:

pushMatrix is not available in this renderer resetMatrix() popMatrix() blendMode()

How is the renderer set? I would like to attach this. Note that this is not headless graphics, but I can then at least create a simple class that does this cleanly and does not open a window.

If I can get the above example working, the next step would be:

PGraphicsJPEG g = new PGraphicsJPEG(320, 200);
g.stroke(...);
g.line(...);
g.save("test.jpg");

However, if generating PDF files, it seems to me I should be able to create something completely headless. OpenGL may require me to connect to a device, but pdf surely does not. I was hoping that perhaps P2D might not, but I suspect that P2D uses OpenGL and therefore is similarly tied to a working graphics display.

Tagged:
Sign In or Register to comment.