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: https://forum.processing.org/two/discussion/12774/embedding-papplet-in-java-jframe

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.

Answers

  • 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 ✓

    @hydrodog===

    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);
              smooth(4);
            }
    
            void setup() {
              //noLoop();
              frameRate(60);
              stroke(-1);
              strokeWeight(1.5);
    
              background(0, 0, 255);
    
              //jframe1 = new JFrame("ecran1");
              this.jframe1 = getJFrame();
              this.jframe1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
              this.jframe1.setResizable(false);
    
    
            }
    
            void draw() {
    
              background(0, 0, 255);
              line(0, 0, width, height);
              if (start) {
                runSketch( new String[] { "--display=1", 
                  "--location=0,0", 
                  "--background=255,0,0", 
                  "--sketch-path=" + sketchPath(""), 
                  "" }, 
                  new AnotherSketch(300, 500, 234, "essai")) ;
    
                start = false;
              }
    
              if (!visible) {
                println("fenetre invisible!!!!");
                //AND WHAT TO DO TO INTERCEPT THE STOP BUTTON???
    
              }
            };
    
    
    
            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);
                smooth(4);
    
    
            }
    
              public void setup() {
                frameRate(1);
                stroke(0, 255, 0);
                strokeWeight(5);
    
                jframe= this.getJFrame();
                jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                  jframe.setResizable(true);
                  //jframe.setUndecorated(true);//too late!!
    
                  jframe.addWindowListener(new java.awt.event.WindowAdapter() {
                  @Override
                  public void windowClosing(java.awt.event.WindowEvent windowEvent) {
    
                   println("sorry to leave you");
    
                 //jframe.dispose();
                   jframe.setVisible(false);
                  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);
    
              }
              @Override
              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.