Embedding Processing 3 Sketch Into Swing

Whether I need to do this or not (IE use a gui framework in processing) is not what i’m asking. I have reasons why I would like to do this, from proof of concept all the way to using processing to do visual rendering inside data driven applications i’m writing.

Anyway, i’ve done plenty of googling before resorting to bothering you all. Everything i’ve come across has not worked for one reason or another. So my question is, is this possible in processing 3? I’d be using the default renderer so no OpenGL contexts.

Thanks for any information, I really do appreciate it.

1 Like

Launching Processing in the default JAVA2D mode creates a swing frame and embeds an AWT canvas (the element that Processing draws on) into it. You can expose the frame and canvas and do anything you want with them: see Show an desktop menu bar and Javax swing JFrame in the main processing PApplet.

Unless you have to use swing, you could use Processing’s JAVAFX mode and create a GUI using JavaFX instead – you can even use SceneBuilder to design the UI and load it into Processing.

2 Likes

Okay so i’ve managed to get a reference to the AWT canvas (smoothcanvas) but when I move the awt surface to my own frame’s JPanel i’m getting a BuffersNotCreated exception.

Here’s my code:

package com.company;

import processing.awt.PSurfaceAWT;
import processing.core.PApplet;
import java.awt.*;

public class Main extends PApplet {

    public static void main(String[] args) {
        App app = new App();


        PApplet.runSketch(new String[]{""}, app);

//        JFrame frame = (JFrame)((processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();

        PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas)app.getSurface().getNative();

        System.out.println(canvas);

        canvas.setPreferredSize(new Dimension(400,400));


        MainForm main = new MainForm("Processing test");
        main.setVisible(true);

        main.mainPanel.add(canvas);

        

    }
}

class App extends PApplet{
    public void settings(){
        size(200, 200);
    }

    public void draw(){
        background(0);
        ellipse(mouseX, mouseY, 20, 20);
    }

    @Override
    public void keyPressed() {

    }
}

Here’s the error

java.lang.IllegalStateException: Buffers have not been created
at sun.lwawt.LWComponentPeer.getBackBuffer(LWComponentPeer.java:519)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)
at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4050)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)
at processing.awt.PSurfaceAWT.render(PSurfaceAWT.java:298)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1548)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

1 Like

Not sure whether moving the surface to your own frame will work. Rather, build on the frame Processing creates.

1 Like