We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was reading some examples and this, this and this.
I wanted to build something like this.
PShape s;
void setup() {
size(400, 400);
s = createShape();
s.beginShape();
s.vertex(10, 10);
s.vertex(200, 200);
s.endShape();
}
void draw() {
background(200);
s.setStroke(#FF0000);
s.setVisible( (millis()/1000%2 == 1) ? true:false);
shape(s);
}
but, with the PShape part of it inside of a class, such as in here:
Line ln;
void setup() {
size(400, 400);
ln = new Line();
}
void draw() {
background(200);
ln.setStroke(#FF0000);
ln.setVisible( (millis()/1000%2 == 1) ? true:false);
ln.draw();
}
class Line {
PShape p;
int strokeColor = #000000;
boolean isVisible = true;
Line() {
p = createShape();
p.beginShape();
p.vertex(10, 10);
p.vertex(200, 200);
p.endShape();
}
void setStroke(int c) {
p.setStroke(c);
}
void setVisible(boolean b) {
p.setVisible(b);
}
void draw () {
if (isVisible) {
shape(p);
}
}
}
Because I would like to have access to some PShape methods, I copied it inside the class, but to avoid making it for every method, I thought of extending PShape class and use super(). But here lies some questions about how to make it right. PLUS, I believe I should NOT depend on passing PApplet to inside a class, right?
//this is not working because I am not sure how to initiate PShape without using PApplet.createShape(xxx);
Line ln;
void setup() {
size(400, 400);
PGraphics g = createGraphics(400, 400);
ln = new Line(this,g); //???
}
void draw() {
background(200);
ln.setStroke(#FF0000);
ln.setVisible( (millis()/1000%2 == 1) ? true:false);
shape(ln);
}
class Line extends PShape {
PApplet p;
Line(PApplet p, PGraphics g) {
super(g,GEOMETRY); //?
this.p = p;
this.beginShape(); //?
this.vertex(10, 10);
this.vertex(200, 200);
this.endShape();
}
}
Answers
Funny, I was just mentioning about PShape class here this morning: :-\"
https://forum.Processing.org/two/discussion/16249/why-isn-t-the-whole-svg-file-transforming#Item_5
A failed PShape subclass experiment: :-q
https://forum.Processing.org/two/discussion/4577/extending-a-class#Item_4
Hey, yes, I checked that example you just posted, but I couldnt actually figure out how to proper call or build it.
So I thought I could build a PShape without depending on the createShape. Plus, the thing of passing PApplet on parameters seems to be not recommend...
I was reading about reflecting a method, or overriding. But I had never used it, i am looking for some examples.
However, here's my successful experiment on subclassing PGraphics class: \m/
https://forum.Processing.org/two/discussion/6884/question-about-pgraphics#Item_3
Although it's a little rusty and won't work on P3. Just fixed it now: :bz
Tab: "JAVA2D_Subclass.pde":
Tab: "Layer.java":
@bontempos, the trick is doing what createShape() or loadShape() would do inside the hacked PShape class.
That's exactly what I did for PGraphics. Its constructor initializes everything just like createGraphics() would. B-)
wow, at first I thought the problem was somehow a matter of my lack of understanding syntaxes, but I see it really needs a lot more hacking than expected lol
I will take a look on it for sure!! thank you very much!! ^:)^