We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Trying to run processing to render graphs on a web server runs into the problem that the machine is headless. While it is easy to create an off-screen renderer, it is not easy to stop the window in the first place.
We could just use the PDF renderer, but since we don't want pdf files it seems like an ugly approach.
I'm willing to edit the code and test a nicer change. Can any developer of the PApplet comment on how to turn off window creation?
Answers
Can you post your code as an MCVE?
I believe I've answered this on StackOverflow, but the basics are that you have to use Processing as a Java library, then just use the PGraphics class to render your image.
I've just added a Wiki page about this: https://github.com/processing/processing/wiki/Running-without-a-Display
For a number of reasons, I don't want to create a virtual X server. I have already written the code to work on a PGraphics. The problem is, it appears that the only legal way to instantiate a PGraphics is through the method createGraphics on PApplet. This is, of course, recursive since the PApplet then need a head to draw its (nonexistent) window on. Forget creating a headless mode, how about telling me how to attach a PGraphics to a PImage?
PGraphics already inherits from PImage! :-B
PGraphics g = new PImage(500,500);
is an error because PImage is the base class, not PGraphics. How to attach a PGraphics to a PImage?
PImage class got a get() method which returns a new clone of it:
https://processing.org/reference/PImage_get_.html
If you merely wanna transform a PImage into a PGraphics,
then createGraphics() using the same width & height dimensions.
Finally background() the PImage into the newly PGraphics clone:
In case you wish to clone an already existing PGraphics instead, the process is very similar.
We only need to find out which renderer it is via isPG(), is2D() & is3D() tests:
This question asks how to draw graphics on a headless workstation. If I run an X server, then obviously, I can run with a PApplet and this is moot. However, it seemed pretty simple to just attach a PGraphics to an image and draw on that instead of a PApplet.
However, @GotoLoop, if the only way of constructing a PGraphics is to have a PApplet do it, then this approach is useless since it won't work on a headless machine.
I looked at the source code for createGraphics, but I could not construct a sequence of operations that would work on an image.
Thanks. On the one hand, creating PGraphicsJava2D is relatively easy. However, it requires that the parent applet be non-null. I have no idea why since we are not drawing to it. In any case, in order to create the PGraphics we have to create a window.
Any idea?
Inside Layer class, constructor's method initialize() is the createGraphics()'s replacement:
AFAIK, only setSize() is essential. We can get away w/o calling any of the rest! \m/
setPath() is to specify the path field for save():
https://processing.org/reference/PImage_save_.html
setPrimary() is to label it is the main canvas. It should always be
false
.And much probably, it is already instantiated as
false
anyways. :-@Dunno what a PGraphics does w/ a PApplet reference.
Maybe it's only necessary when it's setPrimary() to
true
.Just like setPath() & setPrimary(), method setParent() can be omitted w/ peace of mind.
If you wish, you can pass an innocuous
new PApplet()
as argument for it ;))Method ignite() is merely to pre-initialize the newly "hacked" PGraphicsJava2D w/ some nice defaults:
Actually, you can trim that whole subclass a lot!!! >:)