Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed processing 3 into swing

I'm trying to integrate Processing 3 into a swing application, but because PApplet doesn't extend Applet anymore I can't just add it as a component right away.

Is there anyway of embeding a Processing 3 sketch into Swing, it would be enough if I could just open the sketch in a seperate window without the PDE.

like image 248
Cooki3Tube Avatar asked Sep 26 '22 12:09

Cooki3Tube


1 Answers

You can run a sketch from Java by extending PApplet and then using the runSketch() function to run that PApplet. It'll look something like this:

String[] args = {"MyPapplet "};
MyPapplet mp = new MyPapplet ();
PApplet.runSketch(args, mp);

public class MyPapplet extends PApplet {

  public void settings() {
    size(200, 100);
  }
  public void draw() {
    background(255);
    fill(0);
    ellipse(100, 50, 10, 10);
  }
}

Then if you want to get at the underlying component, you have to write code that depends on which renderer you're using. Here's how you'd do it with the standard renderer:

PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();

Once you have the SmoothCanvas, you can remove it from its frame and add it to yours.

like image 198
Kevin Workman Avatar answered Oct 03 '22 14:10

Kevin Workman