Creating detached windows

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.

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:

1 Like