How to integrate java swing and PApplet

edited March 2014 in Using Processing

I have seen a number of demos claiming to integrate PApplet and Swing. For example:

http://wiki.processing.org/w/Swing_JSliders

However this code does not seem to work for me. I cannot even create a JFrame with a button that pops up a PApplet. Is this something that no longer works, since the latest demo I can find is dated 2010, or is there a way to make it work? I would appreciate a pointer to any code example that you know works on Processing 2.1

Tagged:

Answers

  • You cannot use Java Swing components (e.g. JButton, JSlider etc) directly inside a Processing sketch but it is possible to create multiple windows each with its own embedded PApplet, see this post.

    Processing and Java Swing use very different event handling models so they are not easy to integrate. If you want to use multiple windows with their own GUIs you might consider using the G4P library and the GUI Builder tool both can be installed from the Processing Menus.

  • If you will look at the above link (and there are quite a number like it) there are claims that people have been able to put a PApplet as a component in a JFrame. Of course, I can't get it to work.

    I would settle for two separate windows, one with the Swing code, and the other with the PApplet. In any case, neither worked.

    In case I wasn't being clear, this is not a sketch. This is using the Processing libraries in Eclipse.

  • Answer ✓

    OK still not quite clear exactly what you want but I created the class MySwingApp shown below in Eclipse.

    If you run it as an Application then it will open a very small window (JFrame) with a single button (JButton).

    If you then click on the button it will create a new window (JFrame) which has an embedded PApplet object and a button (JButton) and when you click on the button the 5 circles are redrawn in new positions.

    Notice that I have used noLoop() and redraw() to control what is drawn on the PApplet, this is needed to avoid conflict between the event handling between Swing and Processing.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    import processing.core.PApplet;
    
    public class MySwingApp extends JFrame{
    
        JButton btnMakeWindow;
        ControlFrame cf;    
    
        public MySwingApp(){
            this.setLayout(new BorderLayout());
    
            btnMakeWindow = new JButton("Open Window");
            btnMakeWindow.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    cf = new ControlFrame("My Random Circles", 400,400);
    
                }
            });
            add(btnMakeWindow, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    
        public class ControlFrame extends JFrame {
          private ControlApplet papplet;
          private JButton btnCircles;
    
          public ControlFrame(String title, int w, int h) {
            this.setLayout(new BorderLayout());
            papplet = new ControlApplet();
            // So we can resize the frame to get the sketch canvas size reqd.
            papplet.frame = this;
            setResizable(true);
            setTitle(title);
            setLocation(100, 100);
            papplet.appWidth = w;
            papplet.appHeight = h;
    
            // Set the papplet size preferences
            papplet.resize(papplet.appWidth, papplet.appHeight);
            papplet.setPreferredSize(new Dimension(papplet.appWidth, papplet.appHeight));
            papplet.setMinimumSize(new Dimension(papplet.appWidth, papplet.appHeight));
            add(papplet, BorderLayout.CENTER);
            papplet.init();
    
            btnCircles = new JButton("Different Circles");
            btnCircles.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    papplet.redraw();
                };
    
            });
            add(btnCircles, BorderLayout.SOUTH);
            pack();
            setVisible(true);
          }
        }
    
        public class ControlApplet extends PApplet {
    
          public int appWidth, appHeight;
    
          public void setup() {
            size(appWidth, appHeight);
            noLoop();
          }
    
          public void draw() {
            background(200,255,200);
            fill(255, 100, 100);
            stroke(64,0,0);
            strokeWeight(2);
            for(int i = 0; i < 5; i++){
                float x = (float) (Math.random() * width);
                float y = (float) (Math.random() * height);
                float r = (float) (Math.random() * 30 + 20);
                ellipse(x,y,r,r);
            }
          }
        }
    
        public static void main(String[] args) {
            new MySwingApp();
        }
    }
    
  • Thanks, this was great. Now I see what I had to do to stop processing from fighting with swing.

Sign In or Register to comment.