# Creating detached windows

**URL:** https://discourse.processing.org/t/creating-detached-windows/42987
**Category:** Coding Questions
**Created:** [October 16, 2023, 10:48pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987 "2023-10-16T22:48:33Z")
**Posts on this page:** 15
**Page:** 2

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 21, 2023, 4:32pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/22 "2023-10-21T16:32:58Z")

</div>

You could pass it, but draw() doesn’t work in a JFrame. Would need to draw into a JPanel.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 21, 2023, 7:26pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/23 "2023-10-21T19:26:18Z")

</div>

Just tried this basic code on my Linux box with PopOS:

 ![wnd](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/7/0751937a010be320c1b209a4f42255278efe2f58.jpeg)

I can’t even run a resizable window on my Linux system. No wonder the other demo won’t work!

Original demo runs ok on Windows11 and MacOS.

---

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [October 21, 2023, 8:11pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/24 "2023-10-21T20:11:35Z")

</div>

Hi! It should work, because for me it works. Processing needs to do something about that. ☹  
-Libby

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 21, 2023, 9:22pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/25 "2023-10-21T21:22:14Z")

</div>

Please see if you can run this on your system. Note that different PGraphics images are passed to a single PApplet class along with other parameters. The square on each PApplet window is a PGraphics image constructed in the code for the default Processing window.

Addendum: Edited to improve code.

```auto
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

// ****Globals**** //
Window wnd1;
Window wnd2;
PGraphics pg1;
PGraphics pg2;

class Window extends PApplet {
  Frame frame;
  Canvas canvas;

  Point offset = new Point();

  int x, y, w, h;
  color bkgrnd;
  PGraphics inGraphics;

  public Window(int x, int y, int w, int h, color bkgrnd, PGraphics inGraphics) {
    // Needs to be here for use in settings(), setup(), and draw()
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.bkgrnd = bkgrnd;
    this.inGraphics = inGraphics;

    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
    canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative();  
    frame.removeNotify();
    frame.setUndecorated(true);
    frame.addNotify();
    frame.setLocation(x, y);
    
    canvas.addMouseListener(new MouseAdapter() {
      void mousePressed(MouseEvent msEvnt) {
        offset.x = msEvnt.getX();
        offset.y = msEvnt.getY();
      }
    }
    );

    canvas.addMouseMotionListener(new MouseMotionAdapter() {
      void mouseDragged(MouseEvent msEvnt) {
        Point pt = frame.getLocation();
        frame.setLocation(pt.x + msEvnt.getX() - offset.x, pt.y + msEvnt.getY() - offset.y);
      }
    }
    );
  }

  void settings() {
    size(w, h);
  }

  void setup() {
    background(bkgrnd);
    frame.setResizable(true);
  }

  void draw() {
    fill(0, 255, 0);
    circle(50, 50, 50);
    if(inGraphics != null){
     image(inGraphics, 150, 150);
    }
  }
}

// =========== Default Processing Window ========= //

void setup() {
  size(400, 400);
  surface.setTitle("Default Window");
  
  pg1 = createGraphics(100, 100);
  pg2 = createGraphics(150, 150);
  
  pg1.beginDraw();
  pg1.background(0, 255, 0);
  pg1.stroke(0);
  pg1.fill(244, 255, 0);
  pg1.circle(50, 50, 50);
  pg1.endDraw();

  pg2.beginDraw();
  pg2.background(102);
  pg2.stroke(255);
  pg2.line(pg2.width*0.5, pg2.height*0.5, mouseX, mouseY);
  pg2.endDraw();

  wnd1 = new Window(80, 150, 300, 400, color(0, 0, 255), pg1);
  wnd2 = new Window(700, 150, 400, 400, color(255, 255, 0), pg2);
}

void draw() {
  fill(255, 0, 0);
  circle(width/2, height/2, 200);
}

```

**Output:**

 ![pgraphics](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/b/fb4b4ccfe75c8c67237f849c5eb0e09c284bf92b.jpeg)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 22, 2023, 10:27am UTC](https://discourse.processing.org/t/creating-detached-windows/42987/26 "2023-10-22T10:27:06Z")

</div>

Hello @svan,

Code in previous post(s) generated the following errors on W10 with Processing 4.3:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/a/baec672c919113437bbbf5b67421d261b5ff0a8e.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/4/744ab067ec7f9cd40758011675876a21b165a0ea.png)

This seemed to solve the problem most of the time _but not all of the time_:

`frame.createBufferStrategy(2);`

Above insight was gleaned from various references searching for:  
_createBufferStrategy(2)_ (or related)

and I am only sharing this as an initial exploration of this error.

`:)`

---

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [October 22, 2023, 12:22pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/27 "2023-10-22T12:22:58Z")

</div>

Hi! This code as it is works 50% of the time, but does not work with OpenGL aka P2D.  
-Libby

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 22, 2023, 2:48pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/28 "2023-10-22T14:48:54Z")

</div>

I’ve seen similar errors on Linux and also tried to _createBufferStrategy()_, but failed. The two offending lines of code are _removeNotify()_ and _setUndecorated()_. Thanks for the feedback; now we have a good idea where the problem lies if we can just figure out how to fix it.

**Addendum:** Changed source code to create undecorated frame immediately after getting frame and canvas and added _addNotify()_. Also deleted the line to offset the canvas. Mouse handlers switched to canvas from frame; these changes appeared to markedly reduce error messages and will now run on Linux:

```auto
   PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
    canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative();  
    frame.removeNotify();
    frame.setUndecorated(true);
    frame.addNotify();
    frame.setLocation(x, y);

canvas.addMouseListener(new MouseAdapter() {...}

canvas.addMouseMotionListener(new MouseMotionAdapter() {...}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 22, 2023, 2:52pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/29 "2023-10-22T14:52:05Z")

</div>

As a work around, do those auxillary windows have to be undecorated? Try REMMing out _removingNotify()_ and _setDecorated()_.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 23, 2023, 5:56am UTC](https://discourse.processing.org/t/creating-detached-windows/42987/30 "2023-10-23T05:56:55Z")

</div>

This version has been behaving pretty well on a Linux operating system (PopOS). Please see how it runs on your system:

```auto
import java.awt.*;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

Window wnd;
Window wnd1;

class Window extends PApplet {
  Frame frame;
  Canvas canvas;

  Point offset = new Point();

  int x, y, w, h;
  color bkgrnd;

  public Window(int x, int y, int w, int h, color bkgrnd) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.bkgrnd = bkgrnd;

    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
    canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative();
    println(canvas);
    frame.removeNotify();
    frame.setUndecorated(true);
    frame.addNotify();
    frame.setLocation(x, y);
    // canvas.setLocation(0, 28);

    canvas.addMouseListener(new MouseAdapter() {
      void mousePressed(MouseEvent e) {
        offset.x = e.getX();
        offset.y = e.getY();
      }
    }
    );

    canvas.addMouseMotionListener(new MouseMotionAdapter() {
      void mouseDragged(MouseEvent e) {
        Point pt = frame.getLocation();
        frame.setLocation(pt.x + e.getX() - offset.x, pt.y + e.getY() - offset.y);
      }
    }
    );
  }

  void settings() { // Necessary for PApplet
    println(w, h);
    size(w, h); // Sets canvas size
  }

  void setup() {
    background(bkgrnd);
    frame.setResizable(true);
  }

  void draw() {
    fill(0, 255, 0);
    rect(30, 50, 100, 30, 15);
    fill(0); // text color
    textSize(22.0);
    textAlign(CENTER, CENTER);
    text("myBtn", 30, 50, 100, 30);
  }

  void mousePressed() {
    if ((mouseX >= 30) && (mouseX <= 30 + 100) && (mouseY >= 50) && (mouseY <= 50 + 30)) {
      println("button hit.");
    }
  }
}

// ****Default Window**** //

void setup() {
  size(800, 800);
  surface.setTitle("Default Window");
  wnd = new Window(100, 100, 400, 400, color(255, 255, 0));
  wnd1 = new Window(600, 100, 600, 600, color(0, 0, 255));
}

void draw() {
  fill(255, 0, 0);
  circle(width/2, height/2, 200);
}

void mousePressed() {
  println("mousePressed in default window");
}

```

**Output:**

 ![undecorated](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/5/d53aec5114d1be6c37305ee19239854b7a8f7f80.png)

---

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [October 23, 2023, 1:04pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/31 "2023-10-23T13:04:57Z")

</div>

Hi! 🙂 It all works now 90% of the time.

> **I change the code so if you press the mouse down, it creates a new window:**
>
> ```auto
> import java.awt.*;
> import java.awt.Point;
> import java.awt.event.MouseAdapter;
> import java.awt.event.MouseEvent;
> import java.awt.event.MouseMotionAdapter;
> 
> Window wnd;
> Window wnd1;
> 
> class Window extends PApplet {
> Frame frame;
> Canvas canvas;
> 
> Point offset = new Point();
> 
> int x, y, w, h;
> color bkgrnd;
> 
> public Window(int x, int y, int w, int h, color bkgrnd) {
> this.x = x;
> this.y = y;
> this.w = w;
> this.h = h;
> this.bkgrnd = bkgrnd;
> 
> PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
> frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
> canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative();
> println(canvas);
> frame.removeNotify();
> frame.setUndecorated(true);
> frame.addNotify();
> frame.setLocation(x, y);
> // canvas.setLocation(0, 28);
> 
> canvas.addMouseListener(new MouseAdapter() {
> void mousePressed(MouseEvent e) {
> offset.x = e.getX();
> offset.y = e.getY();
> }
> }
> );
> 
> canvas.addMouseMotionListener(new MouseMotionAdapter() {
> void mouseDragged(MouseEvent e) {
> Point pt = frame.getLocation();
> frame.setLocation(pt.x + e.getX() - offset.x, pt.y + e.getY() - offset.y);
> }
> }
> );
> }
> 
> void settings() { // Necessary for PApplet
> println(w, h);
> size(w, h); // Sets canvas size
> }
> 
> void setup() {
> background(bkgrnd);
> rect(10, 10, 100, 100);
> frame.setResizable(true);
> }
> 
> void draw() {
> fill(0, 255, 0);
> rect(30, 50, 100, 30, 5);
> fill(0); // text color
> textSize(22.0);
> textAlign(CENTER, CENTER);
> text("myBtn", 30, 50, 100, 30);
> }
> 
> void mousePressed() {
> if ((mouseX >= 30) && (mouseX <= 30 + 100) && (mouseY >= 50) && (mouseY <= 50 + 30)) {
> println("button hit.");
> }
> }
> }
> 
> // ****Default Window**** //
> 
> void setup() {
> size(800, 800, P2D);
> surface.setTitle("Default Window");
> }
> 
> void makeWindow() {
> try {
> wnd = new Window(100, 100, 400, 400, color(255, 255, 0));
> wnd1 = new Window(600, 100, 600, 600, color(0, 0, 255));
> } catch (IllegalStateException e) {
> print("Trying again...");
> makeWindow();
> }
> }
> 
> void draw() {
> background(255);
> fill(255, 0, 0);
> circle(width/2, height/2, dist(width/2, height/2, mouseX, mouseY));
> }
> 
> void mousePressed() {
> makeWindow();
> println("mousePressed in default window");
> }
> 
> ```

This works like 5 times, then it crashes. I tried building a try catcher for error handling, but that doesn’t seem to do anything!  
Thanks for your patience!  
-Libby

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 23, 2023, 2:17pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/32 "2023-10-23T14:17:52Z")

</div>

> [@Noodlybanan](#):
>
> This works like 5 times, then it crashes

May be running out of memory to maintain all those windows.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 24, 2023, 10:20am UTC](https://discourse.processing.org/t/creating-detached-windows/42987/33 "2023-10-24T10:20:05Z")

</div>

> [@svan](#):
>
> Please see how it runs on your system:

Code from here:  
[https://discourse.processing.org/t/creating-detached-windows/42987/30](https://discourse.processing.org/t/creating-detached-windows/42987/30)

This shows up in console content (no red status in console bar) around 50% of time in W10 Processing 4.3:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/5/155cd6a2a2407aa8109e96ee92b16817989d450d.png)

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 24, 2023, 2:01pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/34 "2023-10-24T14:01:49Z")

</div>

It really doesn’t like that undecorated frame. I don’t recall seeing that on Windows11. When you state that there was _no red status in console bar_ does that mean that the window is formed ok and it’s more of a warning or is the window not formed and it’s more of an exception? If you can’t use the window 50% of the time that’s a problem. I thought that rearranging the code had fixed it, but apparently not on Windows 10. The only way to really solve this is to figure out BufferStrategy and save that before undecorating the frame; so far I haven’t been able to do that. Thanks for the feedback; always appreciated.

Just to verify the above, the following code should run without error on Windows 10:

```auto
import java.awt.*;

Window wnd;

class Window extends PApplet {
  Frame frame;
  Canvas canvas;

  int x, y, w, h;
  color bkgrnd;
  
  public Window(int x, int y, int w, int h, color bkgrnd) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.bkgrnd = bkgrnd;
    
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
    frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
    canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative();

    frame.setLocation(x,y);
    println(canvas);
        
  }

  void settings() { // Necessary for PApplet
    size(w, h); // Sets canvas size
  }

  void setup() {
    background(bkgrnd);
    surface.setResizable(true);
  }

  void draw() {
    fill(0, 255, 0);
    rect(30, 50, 100, 30);
    fill(0);
    textSize(18.0);
    textAlign(CENTER, CENTER);
    text("MyBtn", 30, 50-2, 100, 30);
  }

  void mousePressed() {
    if ((mouseX >= 30) && (mouseX <= 30 + 100) && (mouseY >= 50) && (mouseY <= 50 + 30)) {
      println("button hit.");
    }
  }
}

// ****Default Window**** //

void setup() { 
  size(400, 400);
  surface.setTitle("Default Window");
  wnd = new Window(100, 100, 300, 300, color(0));
}

void draw() {
  fill(255, 0, 0);
  circle(width/2, height/2, 200);
}

void mousePressed() {
  println("mousePressed in default window");
}

```

---

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [October 24, 2023, 4:57pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/35 "2023-10-24T16:57:42Z")

</div>

Hi! To be honest, i think processing doesn’t like undecorated windows at all! They need to fix that, maybe add some prebuilt functions that are also well documented for creating more complexx applications. It would be really nice!  
-Libby!

---

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [October 24, 2023, 7:29pm UTC](https://discourse.processing.org/t/creating-detached-windows/42987/36 "2023-10-24T19:29:37Z")

</div>

Thank you for this code!  
For me this code is completly fine. This code works good on linux too and i can use the Processing Open Gl renderer wich makes me very happy as i can continue to create cool apps. Processing made me really happy so far even with the controversy (Ben fry).  
-Libby

[Previous page](https://discourse.processing.org/t/creating-detached-windows/42987.md?page=1)
