We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Is it possible to create a function that overrides a P5 core function? I'd like to modify how some of the internal stuff works, but without compiling a separate version of the entire P5 core.
More specifically, I'd like to change how line breaks happen when the text()
functions specifies a width and height, by adding additional characters that can cause a break (such as /
and |
).
Here's the function in question: https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L4831
Is this possible? Am I approaching this the right way?
Answers
https://Forum.Processing.org/two/discussion/16314/pshape-inside-a-class-or-extend-pshape#latest
Thanks GoToLoop, but I'm not sure exactly what I do with it. Would I copy the entire class into my code, and have it extend the original? Or can I just override the one function I need inside of that class?
Because
PGraphics.textSentence()
isprotected
(rather thanprivate
) you should be able to extend PGraphics and override with your owntextSentence()
.That said, I vaguely remember encountering frustrating limitations when working with an extended PGraphics class -- but those might have just been limitations in my understanding of Processing at the time.
That was just the way I've found to both
@Override
& add functionality to PGraphicsJava2D.You need to customize it to your own needs though. L-)
B/c Processing relies on createGraphics() in order to instantiate a specific PGraphics subclass, when we extend any of them, we need to write some code which would be done by createGraphics(). #:-S
Hmmm why would one need createGraphics instead of using a constructor? Sorry, the curiosity just bit me....
Kf
Ah, yes, that was the wrinkle! If you extend PGraphics, you also need a version of createGraphics() that will return the extended type.
A PGraphics object can be created using a constructor -- you need to use the createGraphics() function. This is described briefly in the reference ( https://processing.org/reference/PGraphics.html ) and you can find createGraphics code here -- it cascades down to makeGraphics(), which sets the renderer, parent, primary, and path.
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2107