# Create multiple drawing object instances

**URL:** https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289
**Category:** Coding Questions
**Created:** [November 7, 2018, 2:35am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289 "2018-11-07T02:35:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://discourse.processing.org/u/martinh)
#### Post date: [November 7, 2018, 2:35am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/1 "2018-11-07T02:35:25Z")

</div>

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.

Thanks much!  
-Martin

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 3:18am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/2 "2018-11-07T03:18:31Z")

</div>

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 :

```auto
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();
}

```

---

<div class="post-metadata">

### Author: ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://discourse.processing.org/u/martinh)
#### Post date: [November 7, 2018, 3:35am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/3 "2018-11-07T03:35:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 6:22am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/4 "2018-11-07T06:22:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://discourse.processing.org/u/martinh)
#### Post date: [November 7, 2018, 12:28pm UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/5 "2018-11-07T12:28:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 12:56pm UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/6 "2018-11-07T12:56:48Z")

</div>

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().

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 7, 2018, 1:06pm UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/7 "2018-11-07T13:06:35Z")

</div>

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 😅

---

<div class="post-metadata">

### Author: ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://discourse.processing.org/u/martinh)
#### Post date: [November 7, 2018, 6:49pm UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/8 "2018-11-07T18:49:51Z")

</div>

Thanks much, I’ll test it out. I’ll need to draw from with the class so I think extending will make that easier but I’ll try both.

Thanks in finding the bug, it make sense what you had. I couldn’t figure out why it didn’t go. Much appreciated.

---

<div class="post-metadata">

### Author: ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://discourse.processing.org/u/martinh)
#### Post date: [November 9, 2018, 12:52am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/9 "2018-11-09T00:52:18Z")

</div>

Thanks, tested things out and that does as much as the extending method and much easier.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:33am UTC](https://discourse.processing.org/t/create-multiple-drawing-object-instances/5289/10 "2018-11-09T08:33:01Z")

</div>

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.
