# Add PGraphics to a custom method

**URL:** <https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930>\
**Category:** Coding Questions\
**Created:** [June 8, 2019, 6:47pm UTC](https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930 "2019-06-08T18:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 8, 2019, 6:47pm UTC](https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930/1 "2019-06-08T18:47:03Z")

</div>

Hi All,  
I’ve got this code:

```auto
void myrect(float xa,float ya,float xb,float yb){
  float x1=xa;
  if(xa>xb)  
  {
    while(xb<x1){
    x1--;
    point(x1,ya);
    point(x1,yb);
    }
  }
  else if(x1<xb){
    while(x1<xb){
    x1++;
    point(x1,ya);
    point(x1,yb);
  }
}
  if(ya>yb){
  while(ya>yb){
    ya--;
    point(xa,ya);
    point (xb,ya);
  }
}
if(ya<yb){
  while(ya<yb){
    yb--;
    point(xa,yb);
    point (xb,yb);
  }
}
}

```

This method is drawing a rectangle. -Yeah, I know, that there is a rect(), but it’s only allowed to use point() when I’m drawing a rect. So I would like to add a PGraphics to this function. How can I do it?

And how can I fill this rectangle? The fill() doesn’t work here…

Thanks!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2019, 7:09pm UTC](https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930/2 "2019-06-08T19:09:28Z")

</div>

Hello,

Here is how **pg** works - just add more points. Apply idea to your function `myrect()`.

To fill the rect thing, try have one while loop B inside a while loop A in a way that

- A goes over all lines (rows) and
- B over all points (columns) in that row  
like in a grid.

Then use a `point(B_value, A_value);`.

Chrisir

```auto
//https://www.processing.org/reference/PGraphics.html

PGraphics pg;

void setup() {
  size(300, 400);

  pg = createGraphics(40, 40);

  pg.beginDraw();
  pg.background(100);
  pg.stroke(255);
  pg.point(13, 13);
  pg.point(22, 22);
  pg.endDraw();
}

void draw() {
  image(pg, 9, 30);
}

```

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 8, 2019, 8:25pm UTC](https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930/3 "2019-06-08T20:25:40Z")

</div>

Thanks, it looks like its working 😄  
Is it possible to fill everything outside the rectangle?  
 ![re](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5c6efbd735be3bf6e579ec33f1081bad69e589e2.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2019, 8:34pm UTC](https://discourse.processing.org/t/add-pgraphics-to-a-custom-method/11930/4 "2019-06-08T20:34:07Z")

</div>

**Fill the rectangle**

To fill **only** the rectangle:

- A must start at ypos from rect (its upper left corner) and go on until ypos + its height (e.g. 100) is reached
- B must start at xpos of its upper left corner and go on until xpos plus it’s width is reached

**Fill the entire screen**

You can fill the entire screen with background(0); or whatever color. You can apply the above principle also (going from 0 to width of screen etc.).

[and don’t draw a point when its inside the inner rect, if you like. Use `if`…]
