Hi all, new here.
I would like to use P3 to create multiple unique drawing areas. All the examples I’ve seen the window is one large drawing area.
Ideally I’d like to create a class of the ‘drawingArea’ with all the code for drawing within it, then be able to instantiate multiple ‘drawingArea’ objects within the P3D window.
Any thoughts on where to start on this? I’ve looked at multiple P3 examples, book chapter, searches, and can’t seem to find anything close.
You can just use PGraphics. The Canvas itself should be an instance of PGraphics. Create a class called drawingArea with a PGraphics Field inside and draw onto that. For example :
class Area{
PGraphics area;
PVector pos = new PVector();
Area(int x, int y, int w, int h) {
area = createGraphics(w, h);
pos.set(x,y);
}
}
Area a;//creates PGraphic at 10,10 with size 100,100
void setup() {
size(100, 100);
a = new Area(10, 10, 100, 100);
}
void draw() {
image(a.area, a.pos.x, a.pos.y);
}
void mousePressed() {
a.area.beginDraw();
a.area.rect(10,10,50,50);//creates a rect at 20,20 to 70,70
a.area.endDraw();
}
Thanks, I’m understanding and appreciate it, but this line is none too happy and I’m not familiar enough with what you’ve given me to correct on my own:
image(a, a.pos.x, a.pos.y);
help?
note: I’m looking at the PGrahics reference example, and helping, but not sure why image() isn’t liking it when instanced.
Sorry, it’s class Area{, not class Area(){… My bad
Also, you should use a.area, not just a to reference the PGraphics inside…
Changed it, so now it should work.
Thanks much, I’d caught a couple issues including () and missing ;
It’s throwing some Java error now. I’m trying a different approach of extending PGraphics to do what I need. Seems to be working.
One last question on this though, can a class object reference back to the parent object? I tried
parent.test() to see if I could call back, no go. Thoughts?
Are you sure it’s because of the code? … And extending PGraphics should be more complicated than just having a class use it… Well, for the last question, i don’t know what parent.test() should do, or what class it even is… so i can’t tell much about it, but in general you can reference a parent class inside a child… At least as far as i know. Though you should use super().
Ugh… found the problem… apparently you can’t use createGraphics before setup()… I updated the code…
Had it that way, but wanted to make it easier to see…Sorry
You‘re welcome and Yeah, in general it‘s easier to just create a class with an Field of the Type you Would extend than to actually extend it. Though technically it‘s almost the Same, But it saves some trouble. But sometimes it‘s neccessary to extend, so… Well (Not gonna go too deep into extension). It‘s easier to just add something, than to change it.