We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I tried creating a LayerSystem class to manage a Layer class that extends PGraphics and would also hold other properties like opacity, visible, etc. The only problem is that PGraphics is different from everything else in Processing in the way that its instances are created through createGraphics().
The reason I want Layer to extend PGraphics is to be able to write stuff like layer.beginDraw() instead of having Layer hold a PGraphics object like layer.pgraphics.beginDraw().
I cant simply create new Layer() because it is createGraphics(int, int) that sets everything up and creating a PGraphics and casting it to Layer doesn't work.
Any light?
Answers
hm, gotoloop should shed a light
did you try extends?
Composition is often better than inheritance, in general... The added level of indirection is nothing, you can name your field with a short name if that's your problem, like layer.p.beginDraw(). After all, the main PGraphics in PApplet is named g...
If you really want to extend PGraphics or a variant of it, you can do it and have createGraphics() to use it, because it has a renderer parameter, that is not limited to Processing's types, because it uses reflection.
For example, my P8gGraphicsSVG library extends PGraphicsJava2D and declares a SVG constant:
"org.philhosoft.p8g.svg.P8gGraphicsSVG"
I can then use this P8gGraphicsSVG.SVG parameter in createGraphics():
size(400, 400, P8gGraphicsSVG.SVG, "Hearts.svg");
or in beginRecord():
beginRecord(P8gGraphicsSVG.SVG, "ImageTransform.svg");
I see how it would be easier to just compose it.
If I understand it correctly you declared a SVG constant with a path pointing to your P8gGraphicsSVG class itself. That class extends PGraphicsJava2D and all that enabled you to use it as a renderer.
Now:
1 - Can a class extending PGraphics be used as a renderer?
2 - Is it just a matter of declaring a constant pointing to the location of the file?
3 - Could you write a very basic example???
Well, only after some research, HEHE! Now to the issue at hand:
For the surprise of many here, PGraphics is an incomplete class which doesn't have the full implementation to do its job! @-)
Instead, other subclasses of it are tasked for it. That's why we got the selector function createGraphics().
Its job is to instantiate the correct PGraphics renderer subclass w/ the right parameters!
Below's a list of the most common PGraphics renderer implementations:
So, rather than directly extend PGraphics, the path of least resistance is pick up 1 of those renderer subclasses! :ar!
And also implement the rest of the initialization details. Like setParent(), setPath() & setSize(), etc.
And finally, a Layer blueprint, which
extends
PGraphicsJava2D. That should get ya started methinks: (*)thanks! ;-)
@GoToLoop you have definitely answered my initial question and your blueprint works perfectly.
Thanks @PhiLho, your answer got me started. And thanks to @Chrisir for crying for help! :D
Glad you all liked it! Although my version isn't 100% compatible w/ the Processing's API! :|
It doesn't use createGraphics(), neither can be used as a parameter for size() or beginRecord() like @PhiLho's implementation!
But everything else should work alright. I believe it's exclusively for off-screen buffer after all! 3:-O
Nevertheless, I've updated the class to v1.11 now. This time it automatically calls beginDraw(), so we don't have to! \m/
@gotoloop how would you go about setting layer opacity? Would I have to loop the pixels and multiply the alpha or does PGraphicsJAVA2D (or other) have a setting for that?
A PGraphics, and thus a Layer, starts off as 100% transparent black already.
We can use
clear();
orbackground(0, 0);
to reset to 100% transparent black again! *-:)Also, read about the
color
datatype:http://processing.org/reference/color_datatype.html
BtW, I've got the following example that sets the alpha value of a squared area: :-bd