Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Processing 3 with P3D embedded in JPanel

$
0
0

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.


Viewing all articles
Browse latest Browse all 2896

Trending Articles