Processing in Swing

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 don’t want to use G4P or other libraries, as I said, the idea is to draw swing UI on top of the game

If that can’t be done for whetehver reason, it would be cool to know a workaround or similar

I can also make side windows and make the UI there but that would be suboptimal

Thank you!

1 Like

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

boolean click, pclick;
void mouse(){
  if (mousePressed && pclick){
    pclick=false;
    click=true;
  } else if (!mousePressed) {
    pclick = true;
    click=false;
  } else {
    click=false;
  }
}

From here you can customize how the button is drawn from button(), and add a string variable for the button label.

But again, I dont know if there is any proper UI stuff, I just prefer this for its customization possibility’s.

1 Like

If you are using the standard JAVA2D mode then the sketch is already running in a JFrame and you can get a reference to it with

JFrame frame;

frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();

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.

1 Like

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.