Embed processing 3 into swing

徘徊边缘 提交于 2019-12-06 05:13:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33101812/embed-processing-3-into-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!