# Why multiple windows can't show sketch correctly?

**URL:** <https://discourse.processing.org/t/why-multiple-windows-cant-show-sketch-correctly/26659>\
**Category:** Coding Questions\
**Created:** [December 31, 2020, 9:29pm UTC](https://discourse.processing.org/t/why-multiple-windows-cant-show-sketch-correctly/26659 "2020-12-31T21:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IpNeNaEcRe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ipnenaecre/32/11223_2.png) [@IpNeNaEcRe](https://discourse.processing.org/u/IpNeNaEcRe)\
**Post date:** [December 31, 2020, 9:29pm UTC](https://discourse.processing.org/t/why-multiple-windows-cant-show-sketch-correctly/26659/1 "2020-12-31T21:29:47Z")

</div>

I want to create multiple windows, one to interact (put slide bar in it) and other to show the sketch. Now I can draw line/rectangle/ellipse directly in the second window. When these things in another class and use the class in the second window, they won’t show correctly. Does the coordinate system different in another PApplet? How can I solve this?

```java
MultiWindows sketch2; 
Rec rec1;

void settings() {
  size(400, 400);
}

void setup() {
  surface.setLocation(0, 0);
  surface.setTitle("Sketch 1");

  rec1 = new Rec(200, 200);
  sketch2 = new MultiWindows();
}

void draw() {
  background(255);
  stroke(255, 0, 0);
  rec1.display();
}

class MultiWindows extends PApplet {
  Rec rec2;

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

  public void settings() {
    size(400, 400);
  }

  public void setup() {
    surface.setLocation(400, 0);
    surface.setTitle("Sketch 2");
    rec2 = new Rec(200, 200);
  }

  public void draw() {
    background(255);
    stroke(0, 255, 0);
    rec2.display(); // won't work
    rect(200, 200, 40, 40); // this works fine
  }
}

class Rec {
  int x, y, r;

  Rec(int x, int y) {
    this.x = x;
    this.y = y;
    r = (int)random(20, 40);
  }

  void display() {
    rectMode(CENTER);
    noFill();
    rect(x, y, r, r);
    r = (int)random(20, 40);
  }
}

```

And I see from Internet that only one window can use OPENGL renderer such as P2D/P3D, why this [multi-window OpenGL example](https://github.com/processing/processing/issues/5255) works fine?

---

<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:** [January 1, 2021, 4:49am UTC](https://discourse.processing.org/t/why-multiple-windows-cant-show-sketch-correctly/26659/2 "2021-01-01T04:49:30Z")

</div>

> [@IpNeNaEcRe](#):
>
> ```auto
> class Rec {
> int x, y, r;
> 
> Rec(int x, int y) {
> this.x = x;
> this.y = y;
> r = (int)random(20, 40);
> }
> 
> void display() {
> rectMode(CENTER);
> noFill();
> rect(x, y, r, r);
> r = (int)random(20, 40);
> }
> }
> 
> ```

**rectMode()**, **noFill()**, **rect()**, etc. are all methods from class PApplet:  
[http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html)

However, directly invoking them w/o using the dot `.` operator:

> **[. (dot) / Reference](https://processing.org/reference/dot.html)**
>
> Provides access to an object's methods and data. An object is one instance of a class and may contain both methods (object functions) and data (object variables and constants), as specified in the cla…

Implies that they act upon the sketch’s main PApplet instance.

If you need a class to act upon other PApplet instances you’re gonna need to request & store their reference.

This is your class Rec modified to demand a PApplet reference right on its constructor:

```auto
static class Rec {
  static final int MIN_DIAM = 20, MAX_DIAM = 40;

  PApplet p;
  int x, y;

  Rec(PApplet p, int x, int y) {
    this.p = p;
    this.x = x;
    this.y = y;
  }

  void display() {
    p.noFill();
    p.circle(x, y, p.random(MIN_DIAM, MAX_DIAM));
  }
}

```

Obviously you’re gonna need to pass a PApplet ref. via keyword `this` when creating a Rec object now:  
`rec1 = new Rec(200, 200);` - - \> `rec1 = new Rec(this, 200, 200);`  
`rec2 = new Rec(200, 200);` - - \> `rec2 = new Rec(this, 200, 200);`

BtW, in order to avoid inadvertently invoking other PApplet methods w/o the dot `.` operator, it’s a good idea to declare such classes w/ the keyword `static`:

> **[static / Reference](https://processing.org/reference/static.html)**
>
> Keyword used to define a variable as a "class variable" and a method as a "class method." When a variable is declared with the static keyword, all instances of that class share the same variabl…

`class Rec {` - - \> `static class Rec {`  
`class MultiWindows extends PApplet {` - - \> `static class MultiWindows extends PApplet {`

---

<div class="post-metadata">

**Author:** ![IpNeNaEcRe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ipnenaecre/32/11223_2.png) [@IpNeNaEcRe](https://discourse.processing.org/u/IpNeNaEcRe)\
**Post date:** [January 1, 2021, 7:43am UTC](https://discourse.processing.org/t/why-multiple-windows-cant-show-sketch-correctly/26659/3 "2021-01-01T07:43:21Z")

</div>

Thanks a lot, it helps.
