Is there any way to get Processing to render into a JFrame (or something along those lines)?
I’ve been reading and trying for myself but coudn’t get it to work
The idea is to be able to make a UI using swing on top of the image rendered by processing
Like in the image but instead of with my own ui, with swing
I am unsure about any actual proper ui things for processing, this is what I have always done:
in draw:
mouse();
if (button(20,20, 100, 20)) {
// do something
}
A class for drawing and detecting the button presses
boolean button(int x, int y, int w, int h) {
if (mouseX> x && mouseX< x + w && mouseY > y && mouseY<y+h) { // if mouse is inbounds
fill(0); // draw button in hovered state
rect (x,y,w,h);
if (click) {
return true;
}
} else {
fill(255); // draw button in non hovered state
rect (x,y,w,h);
}
return false;
}
A method for getting a single frame of true for each click
With the frame reference you should be able to add Swing controls. to the sketch but personally I would not recommend it because there is likely to be conflicts between Processing’s event handling thread and Swings event handler.
A better approach is probably to use the PGraphics class inside a Swing application and avoid using the PApplet class which would get rid of Processing’s event thread altogether.
Sorry for the slow response but I cannot get your solutions to work.
The first one I tried and I probably did something wrong:
import javax.swing.*;
import processing.awt.PSurfaceAWT;
JFrame ventana;
void setup() {
size(640, 360, JAVA2D);
ventana = (JFrame) ((PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
ventana.add(new JTextField());
// Tried this to see if it was an issue with redrawing the frame but no luck
ventana.revalidate();
ventana.repaint();
}
I haven’t tried the second solution due to my limited knowledge of Processing working as a Java library.
If you can, please write me a small working example of both solutions so I can study how they work.
Thank you and have a nice day.