# Running PGraphics on a second window

**URL:** <https://discourse.processing.org/t/running-pgraphics-on-a-second-window/13563>\
**Category:** Libraries\
**Created:** [August 23, 2019, 7:18pm UTC](https://discourse.processing.org/t/running-pgraphics-on-a-second-window/13563 "2019-08-23T19:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [August 23, 2019, 7:18pm UTC](https://discourse.processing.org/t/running-pgraphics-on-a-second-window/13563/1 "2019-08-23T19:18:57Z")

</div>

I have the following situation. I have a PGraphics that’s running without problems in the main window. I want to remove this PG from the main window and simply display it on a second window.

All the calculation and ControlP5 interaction are in the main thread so i won’t have any problems comunicating changes between threads. So i’ve checked the forums for an answer and tried a few of the solutions presented. but actually none of them worked. how do i pass the PG to the second window? i’ve tried sending the PGraphics in the constructor and nothing happened. Should i pass the PG to the second window in something like this:

```auto
win.draw(pg)

```

this is the situation i have now:

 ![01%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7f8cd5dcbe19dc5baaacc29f8b5327f8dfc786eb.png)

here’s the basic code

```auto
import controlP5.*;
import com.hamoid.*;

ControlP5 cp5;
VideoExport videoExport;

//second window
PWindow win;

ParticleSystem ps;
//PGraphics
PGraphics pg;
int w=2400;
int h=1920;
int pgOrigin=400;

//Particle Control
int speedX=0;
int speedY=0;
int spawn=0;
int pw=w;
int ph=30;
int spawnX=0;
int spawnY=h;

color fillColor=color(255);

//video export
boolean isRecording=false;

String filename="particleMaker";

void settings() {
  size(1020, 960, P2D);
  smooth();
}

void setup() {
  //setup ControlP5  
  controlP5Setup();

  //setup PG
  println("building PGraphics");
  pg=createGraphics(w, h, P2D);

  ps=new ParticleSystem();
 
  win=new PWindow();

  //setup videoexport
  videoExport = new VideoExport(this, filename+".mp4", pg);
  videoExport.startMovie();
}

void draw() {
  background(32);

  if (spawn>0 && frameCount % spawn==0) {
    ps.addParticle();
  }

  pg.beginDraw();
  pg.background(0);

  ps.run();

  pg.endDraw();

  image(pg, 400, 0, 600, 480);

  //saves();
  if (isRecording) {
    videoExport.saveFrame();
  }
}

void saves() {
  pg.save("saves/"+filename+"C/"+filename+"C-"+frameCount+".png");
}

void keyPressed() {
  if (key=='s') saves();

  if (key == 'r' || key == 'R') {
    isRecording = !isRecording;
    println("Recording is " + (isRecording ? "ON" : "OFF"));
  }

  if (key == 'q') {
    videoExport.endMovie();
  }
}

```

PWindow code

```auto
public class PWindow extends PApplet {
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }
  void settings() {
    size(600, 480);
  }

  void setup() {
    background(0);
  }

  void draw() {
    image(pg, 0, 0, 600, 480);
  }
}
```

---

<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:** [August 23, 2019, 7:55pm UTC](https://discourse.processing.org/t/running-pgraphics-on-a-second-window/13563/2 "2019-08-23T19:55:43Z")

</div>

- [Forum.Processing.org/two/discussion/25799/how-to-draw-a-rect-in-a-thread#Item\_2](http://Forum.Processing.org/two/discussion/25799/how-to-draw-a-rect-in-a-thread#Item_2)

> [@Problems when trying to draw to a P3D PGraphics when called from a thread?](https://discourse.processing.org/t/problems-when-trying-to-draw-to-a-p3d-pgraphics-when-called-from-a-thread/6301/4):
>
> Although it’s pretty much a hack, here’s the workaround I’ve come up w/: crazy_face /\*\* \* Threaded PGraphics II (v1.0.1) \* GoToLoop (2018/Dec/07) \* \* https://Discourse.Processing.org/t/ \* problems-when-trying-to-draw-to-a-p3d-pgraphics- \* when-called-from-a-thread/6301/4 \* \* https://Forum.Processing.org/two/discussion/25799/ \* how-to-draw-a-rect-in-a-thread#Item\_2 \*/ static final int W = 250, H = 200, FPS = 10; Layer layer; void settings() { size(W, H, JAVA2D); smooth(3); } v…

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 12, 2019, 7:01pm UTC](https://discourse.processing.org/t/running-pgraphics-on-a-second-window/13563/3 "2019-09-12T19:01:31Z")

</div>

I’ve used a workaround. not that classy or hackable, but used the good old syphon/spout solution.  
since i was already outputing via syphon, it just another pipe in the system.
