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:
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