# Is it possible to draw main graphics on a PGraphics?

**URL:** https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055
**Category:** Processing
**Created:** [April 7, 2019, 8:09pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055 "2019-04-07T20:09:06Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [April 7, 2019, 8:09pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/1 "2019-04-07T20:09:06Z")

</div>

Is it possible to draw `g` on a PGraphics?  
It should be done in P3D and it should be done without using loadPixels();

So something like this:

```auto
PGraphics pg;

void setup() {
  size(512, 512, P3D);  
  
  pg = createGraphics(256, 256, P3D);
  
}

void draw() {
  background(0);
  fill(255,255,0);
  circle(width/2, height/2, 256);
  
  pg.beginDraw();
  pg.image(g, 0, 0, pg.width, pg.height);
  pg.endDraw(); 
}

```

I tried different things with pre and post and endDraw but so far no luck.

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [April 8, 2019, 8:50am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/2 "2019-04-08T08:50:04Z")

</div>

I try understand what you want but is not clear, are you expected somethinf like that ?

```auto
PGraphics pg;
PImage img;

void setup() {
  size(512, 512, P3D);  
  pg = createGraphics(256, 256, P3D);
  img = createImage(pg.width, pg.height, RGB);
}

void draw() {
  background(0);
  fill(255,255,0);
  circle(width/2, height/2, 256);
  
  g.loadPixels(); // need to load to don't have have null pixels
  img.copy(g,0,0,pg.width,pg.height, 0,0,pg.width,pg.height);
  
  fill(255,0,0);
  circle(width/2, height/2, 256);
  
  image(img,0,0);
  /*
  pg.beginDraw();
  pg.image(img, 0, 0, pg.width, pg.height);
  pg.endDraw();
  */
  pg.loadPixels();
  println("pg",pg.pixels); // return null
}

```

 ![48](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f647e569d57fbd74753e7cfa1442fc30f1892b60.jpeg)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 10, 2019, 4:25am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/3 "2019-04-10T04:25:20Z")

</div>

> [@clankill3r](#):
>
> it should be done without using loadPixels()

Can you explain why this is a requirement? This seems odd, since you want to access the pixels array of g – and that is what loadPixels is for.

> **[loadPixels() / Reference](https://processing.org/reference/loadPixels_.html)**
>
> Loads the pixel data of the current display window into the pixels\[\] array. This function must always be called before reading from or writing to pixels\[\]. Subsequent changes to the disp…

You can try to skip this sometimes, but are explicitly warned against it in the reference:

> Certain renderers may or may not seem to require **loadPixels()** or **updatePixels()**. However, the rule is that any time you want to manipulate the **pixels[]** array, you must have previously called **loadPixels()** , and after changes have been made, call **updatePixels()**. Even if the renderer may not seem to use this function in the current Processing release, this will always be subject to change.

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [April 10, 2019, 8:18am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/4 "2019-04-10T08:18:24Z")

</div>

Hi! What does no luck mean in this case?

I didn’t look at the source, but it could be that Processing automatically calls `beginDraw` and `endDraw` for the default `g` PGraphics at the begining and end of `draw()`. Maybe that means you can’t draw `g` onto something else because `endDraw` hasn’t been called yet?

Knowing why you want to do this may help suggest an alternative 🙂

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [April 22, 2019, 9:43am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/5 "2019-04-22T09:43:00Z")

</div>

I made a renderer for the terminal:

 ![](https://raw.githubusercontent.com/clankill3r/p5_terminal_graphics/master/data/fox.gif)

I don’t want to use `loadPixels` on the main graphics cause it can be really slow depending on it’s size.  
So what I do now is make a smaller graphics, then I draw `g` on that smaller graphics and load the pixels from that instead. I can only get this working in `post` which is not safe to draw, but it works…

Here is the repository if anyone is interested. If you made something cool then please post a image.

> **[clankill3r/p5\_terminal\_graphics](https://github.com/clankill3r/p5_terminal_graphics)**
>
> Contribute to clankill3r/p5\_terminal\_graphics development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [April 22, 2019, 11:05am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/6 "2019-04-22T11:05:51Z")

</div>

I really like this kind of hacks that push what’s possible or reasonable 🙂

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 22, 2019, 1:29pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/7 "2019-04-22T13:29:12Z")

</div>

> [@clankill3r](#):
>
> I made a renderer for the terminal

👏 👏 👏

Wild.

Does it also work as system shell output when called from processing-java?

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [April 22, 2019, 4:29pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/8 "2019-04-22T16:29:35Z")

</div>

I will check later, if it doesn’t I can probably make it work.  
That will make it also more friendly for PDE users that don’t have additional IDE’s installed.

O yeah the thing is Max / Linux only, this has to do that I put the terminal in character mode. And I’m tired of making things cross platform.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 22, 2019, 5:20pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/9 "2019-04-22T17:20:21Z")

</div>

> [@clankill3r](#):
>
> I’m tired of making things cross platform.

That is totally fair – I am too. Although a “Terminal Mode” for PDE would be pretty cool.

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [April 24, 2019, 7:46am UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/10 "2019-04-24T07:46:06Z")

</div>

Some people wanted to work on the console:

> <https://github.com/processing/processing/issues/5779>

Not sure if they started yet, and if they will, not sure if they will follow specification to make a descent terminal (supporting ANSI escapes, single character mode etc.)

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [April 24, 2019, 4:18pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/11 "2019-04-24T16:18:08Z")

</div>

It might be worth having a look at the JLine library for this. Should enable it to be fully cross-platform too.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 24, 2019, 8:29pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/12 "2019-04-24T20:29:00Z")

</div>

I wasn’t familiar with JLine. Looks like it is here:

> **[jline/jline3](https://github.com/jline/jline3)**
>
> JLine is a Java library for handling console input. - jline/jline3

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [April 25, 2019, 1:23pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/13 "2019-04-25T13:23:32Z")

</div>

Yes, sorry, should have added a link! It’s also the library used by the Java shell in the JDK.

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [July 18, 2020, 8:55pm UTC](https://discourse.processing.org/t/is-it-possible-to-draw-main-graphics-on-a-pgraphics/10055/14 "2020-07-18T20:55:46Z")

</div>

I updated the project, more here:

> [@P5\_terminal\_graphics](https://discourse.processing.org/t/p5-terminal-graphics/22712):
>
> I posted this before in a topic related to a question. The last two days I have spent some time cleaning up the project and getting rid of dependencies. Anyway, what I made is a renderer that can be set using: size(1120, 850, P5\_Terminal\_Graphics.TERMINAL); And then when an external console is used, then that can be used as a display. You need an external editor. VScode is now way easier to get working with processing then it was before. If you like to try it out a…
