# PGraphics and PApplets

**URL:** <https://discourse.processing.org/t/pgraphics-and-papplets/1227>\
**Category:** Coding Questions\
**Created:** [June 25, 2018, 12:31am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227 "2018-06-25T00:31:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [June 25, 2018, 12:31am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/1 "2018-06-25T00:31:02Z")

</div>

Hello. Im trying to do a preview output of main buffer Graphic in a extra pApplet.

Here my code:

```auto
layerWindow p;
LayerManager layer;

void settings(){
size(1280, 720, P3D);
}

void setup(){
  p = new layerWindow();
  layer = new LayerManager();
}

void draw(){

}

class LayerManager {

  ArrayList<PGraphics>layers;

  LayerManager() {

    layers = new ArrayList<PGraphics>();

    layers.add(createGraphics(width, height, P3D));
    
  }

  void update(PApplet p) {

    layers.get(0).beginDraw();
    //buffer.clear();
    //buffer.shader(vs.get(0).shader);
    //layers.get(0).hint(DISABLE_DEPTH_TEST);
    layers.get(0).hint(DISABLE_OPTIMIZED_STROKE);
    layers.get(0).hint(DISABLE_DEPTH_MASK);
    layers.get(0).sphere(100);
    layers.get(0).endDraw();
 
      tint(255, 50);
      image(layers.get(0), 0, 0);

    
  }

}

public class layerWindow extends PApplet {  

  layerWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(1280, 720, P2D);
  }

  void setup() {
  }

  void draw() {
    layer.update(this);
  }
}

```

but it return the following error:

You are trying to draw outside OpenGL’s animation thread.  
Place all drawing commands in the draw() function, or inside  
your own functions as long as they are called from draw(),  
but not in event handling functions such as keyPressed()  
or mousePressed().

How can i draw a PGraphic image in a extra Applet? Thanks!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 25, 2018, 12:51am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/2 "2018-06-25T00:51:54Z")

</div>

I think you are not allowed for multiple PApplets to access the P2D/P3D renderers at the same time. In other words, only one applet is allowed to access OpenGL per sketch. Do you need the OpenGL renderer for your task?

Kf

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [June 25, 2018, 12:53am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/3 "2018-06-25T00:53:31Z")

</div>

I do others things with a P2D in the extra applet and it works…  
i need a copy of that PGraphics buffer and feed into the main canvas and in an extra Applet.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 25, 2018, 1:29am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/4 "2018-06-25T01:29:02Z")

</div>

- Only 1 Thread can write to a PGraphics at the same time! 🤐
- Although many can read from 1 at the same time. 😀
- However PGraphics multithreading writing doesn’t seem it’s the exact problem there. 😵
- The only instance of LayerManager is created by the main PApplet and its reference stored in its global field _layer_.
- Inside its constructor, a P3D **createGraphics()** is invoked, w/ its OpenGL context bound to the main PApplet.
- However, you attempt to change its attributes under another PApplet instance! 😰
- You’re gonna need to ask the main PApplet, which owns that PGraphics, to do that instead! 🙃

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [June 25, 2018, 2:49am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/5 "2018-06-25T02:49:37Z")

</div>

thanks! any idea how can i do it? or some starting point to research?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 25, 2018, 2:59am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/6 "2018-06-25T02:59:34Z")

</div>

- Make your other PApplet have its own PGraphics.
- Then your main PApplet can display it via **image()**.

---

<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:** [June 25, 2018, 10:16am UTC](https://discourse.processing.org/t/pgraphics-and-papplets/1227/7 "2018-06-25T10:16:25Z")

</div>

Using multiple PApplets inside the same JVM with OpenGL is a recipe for disaster! Unfortunately, static state in the renderer causes instability and/or crashes, somewhat dependent on your drivers and what exactly you’re doing.

The best way to do this, although unfortunately not platform-neutral, would be to use the Syphon (macOS) and/or Spout (Windows) libraries to share graphics across processes. This is done via texture sharing on the GPU, so will perform better than anything you do to share image data across PApplets inside Processing anyway. Annoyingly there’s no Linux equivalent though.
