Write to PGraphics in class

I'd like to build a class that contains a PGraphics object and write to it from outside the class (in setup(), draw(), etc). I can do this now, but it's clunky:

thing.pg.beginDraw();
thing.pg.rect(0,0, 10,10);
thing.pg.endDraw();

I'd like to be able to do something more like this:

thing.beginDraw();
thing.rect(0,0, 10,10);
thing.endDraw();

I know I can create new functions for rect(), etc inside the class, but that's ugly. I found this post, which offers some hope, but not sure how to make it work in my case.

Tagged:

Answers

  • You can make your class extend PGraphics, provided you can make it somehow fit into your class scheme.

  • Thanks both of you – I did see the "layer" post that GoToLoop posted. But the thread is very confusing and I don't have much experience with extending classes. Could you show me a very simple class that does this?

    (For example, the exampled linked to above also gets the parent applet and does some other stuff – is that necessary too?)

  • edited August 2016
    • That utility method getEnclosingPApplet() isn't necessary.
    • It's just a hack to avoid requesting the sketch's PApplet reference.
    • Method ignite() can also go away too.
    • But as I said back then, that Layer class wasn't meant to be understood.
    • Rather to be moved to a separate tab and stay hidden from view.
    • For it is just a base to make our own PGraphicsJava2D based subclasses.
    • And also a replacement for what createGraphics() actually does underneath it.
    • For a basic subclass example, take a look @ CustomPGraph:
      https://forum.Processing.org/two/discussion/6884/question-about-pgraphics#Item_3
Sign In or Register to comment.