How to add a processing 3 applet to a JFrame

edited May 2016 in How To...

It used to be that a processing 2 applet could be added to a JFrame because it was inherited from Applet which inherits from JPanel. Now it's not.

I see this long discussion:

but I do not see how to create a JFrame in Java, and add a PApplet as a component. Particularly important is that the PApplet be able resize when told to do so by the layout manager.

Is there any way to do this without using runSketch? It's a real hack to have to have the drawing area create the window.


  • Is there any way to do this without using runSketch?

    Not that I know of. In PS2 the window was always a JFrame no matter which renderer i.e. JAVA2D, P2D or P3D so it was fairly easy to create a JFrame and embed a PApplet in it. With PS3 JFrame is only used with the JAVA2D (default) renderer and uses GLWindow with P2D and P3D.

  • I realize this cleaned up the codebase, but not being able to embed into a java window with menus and buttons is a major pain. The only thing I was able to suggest to a student is to re-implement the entire Component hierarchy in processing.

  • Answer ✓


    i was in the discussion you talked about; It was GoToLoop who has given the best way to do what you want; after that i tried by myself and got some results. In the code below you can see that i can create 2 windows with different size, locations, color and so on. But i never found the way to intercept the close button event in order that closing the second window you dont close the first one. If you or anybody can give an hint....As for me, finally, for this work i decided to work in the old P2 way and my app can create 200 windows with 200 instances from Applet...

            import processing.awt.PSurfaceAWT;
            import javax.swing.JFrame;
            import java.awt.event.*;
            JFrame jframe1;
            boolean start = true;
            boolean visible = true;
            void settings() {
              size(300, 300, JAVA2D);
            void setup() {
              background(0, 0, 255);
              //jframe1 = new JFrame("ecran1");
              this.jframe1 = getJFrame();
            void draw() {
              background(0, 0, 255);
              line(0, 0, width, height);
              if (start) {
                runSketch( new String[] { "--display=1", 
                  "--sketch-path=" + sketchPath(""), 
                  "" }, 
                  new AnotherSketch(300, 500, 234, "essai")) ;
                start = false;
              if (!visible) {
                println("fenetre invisible!!!!");
            public JFrame getJFrame() {
              PSurfaceAWT surf = (PSurfaceAWT) getSurface();
              PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas) surf.getNative();
              return (JFrame) canvas.getFrame();
            class AnotherSketch extends PApplet {
              int sizeX, sizeY, bg2, index;
              int delai = 15000;
              JFrame jframe;
              AnotherSketch(int sX, int sY, int back, String t) {
                sizeX = sX;
                sizeY = sY;
                bg2 = back;
              public void settings() {
                size(displayWidth/2, displayHeight/2, JAVA2D);
              public void setup() {
                stroke(0, 255, 0);
                jframe= this.getJFrame();
                  //jframe.setUndecorated(true);//too late!!
                  jframe.addWindowListener(new java.awt.event.WindowAdapter() {
                  public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                   println("sorry to leave you");
                  visible = false;
                //jframe.pack();//if you dont set the size in constructor
              public JFrame getJFrame() {
                PSurfaceAWT surf = (PSurfaceAWT) getSurface();
                PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas) surf.getNative();
                return (JFrame) canvas.getFrame();
              public void draw() {
                background((color) random(#000000));
                line(width, 0, 0, height);
              public void stop(){
                println("jestoppe la fenetre");//never fired...
  • This hack shows what a terrible decision has been made in the name of performance. I want to try to rip apart the PApplet class and create something that will allow easy integration with other parts of Java.

  • If you are creating a desktop application with multiple windows then try G4P, it supports multiple windows and you can close the extra windows individually. Also you can specify a function to be executed when the extra window is closed.

    Install G4P and try the Mandelbrot example.

    Note there are some issues related to windows closing (in P3) this web page has links to info about using G4P windows.

Sign In or Register to comment.