Processing 3 with P3D embedded in JPanel

edited July 2016 in Library Questions

In my current project, I need to embed a Processing 3 canvas in my Java Swing application. I could find some good examples for P2D renderer, which worked perfectly fine; however, my application is using P3D renderer for displaying 3D models.

After a thorough search on the Internet and reading the source codes, I could get a solution, which does not work properly.

Here is a baby example of what I have so far followed by the screenshots of my issue (the example uses JFrame for simplicity and conciseness).

public class Visualiser3D extends PApplet {

    private PeasyCam camera;

    @ Override
    public void settings() {
        size(1000, 800, P3D);
    }

    @ Override
    public void setup() {
        camera = new PeasyCam(this, 0, 0, 0, 1500);
    }

    @ Override
    public void draw() {
        camera();        // force default coordsys
        camera.feed();   // manually set peasycam
        background(30);
        sphere(200);    // draw a sphere
    }

    public static void main (String[] args) {
        // prepare JFrame
        JFrame frame = new JFrame("Processing in JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // run Processing
        Visualiser3D mp3d = new Visualiser3D();
        PApplet.runSketch(new String[]{"MyPapplet 3D"}, mp3d);
        PSurfaceJOGL pSurfaceJOGL = (PSurfaceJOGL) mp3d.getSurface();
        pSurfaceJOGL.initOffscreen(mp3d);
        // add canvas to JFrame (used as a Component)
        NewtCanvasAWT canvas = (NewtCanvasAWT) pSurfaceJOGL.getComponent();
        frame.add(canvas, BorderLayout.CENTER);        
        pSurfaceJOGL.startThread();

        // display
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
} 

Here are the screenshots of what I get. The actual processing window keeps running, and if I close it programmatically, the entire application gets closed. I would like to have the Processing canvas embedded inside of my Java Swing application without any other open windows.

JFrame with inserted Processing Canvas Processing Window running

I hope that you will be able to help me do it properly. Thank you very much.

Answers

  • Anyone, could you at least drop in some ideas. Experts, where are you all?

  • The code you provide is for Processing 2.

    In Processing 2 it was straightforward to create a class that inherited from PApplet then embed an instance of this class into a JFrame. In Processing 3 this is not possible because PApplet no longer inherits from a windowed component.

    In Processing 3 when you run a sketch using P2D or P3D then it is opened in a OpenGL window not a JFrame.

  • In Processing 3 this is not possible because PApplet no longer inherits from a windowed component.

    The irony of it is that the multiple sketch technique applied to P3 has always been available in Processing. That is, it would work in the ancient Processing version 1.5.1 for example. ;))

  • Thank you for your replies. So, I guess, the solution would be either to use an older version of Processing or to run the sketch in its native window. It is a big pity that such an easy-to-use and powerful visualizer as Processing does not allow for JFrame integration.

  • It is possible to use multiple windows (e.g. 1-JAVA2D 1-P3D) in Processing 3 with the G4P library but it means moving your code from a Java Swing application.

Sign In or Register to comment.