I really like Processing. Processing is a Visual Programming language, and it is based on Java. It is designed to run either as an Applet, a Standalone App, or as a Java Component; running a Processing Sketch as a Component is the one with less documentation, but probably one of the most interesting and powerful applications of it.
This is a small guide on setting up a Processing Sketch as a Java component, ready to use on your Swing or AWT desktop applications.
Processing Sketch Running on JFrame

I am assuming you have a basic notion of Object Oriented Programming, and preferably a base on Java, as it is obvious, you will need basic Processing skills.
The first Step is to download Processing, I will not go too deep onto this, but just make sure you get the latest version for your OS.
You will also need the Java Development Kit (JDK) and preferably an IDE (Integrated Development Environment) to help you along the way, two popular ones are NetBeans and Eclipse.

After your environment is all set up, you will need a to create a new Project, just follow the instructions for your IDE.

The second step is to add to your project the library core.jar, which contains the basic Processing functionality. This library is found on the lib/ folder of the main Processing folder. For Linux this is located wherever you installed Processing, on Windows it is on Program Files/Processing, and in Mac it is located inside Processing.app, just Select the app, select File Menu > Show Package Contents and then open Resources folder.

Once you’ve added the library to your project, it’s real easy to start using it. PApplet is the class which Processing Applets extend, and it holds 2 very important methods, which you must implement to make a working Processing Sketch, this ones are void setup() and void draw().
You might note that this ones are quite similar to the ones on an average interactive Processing sketch, and you are totally right, in fact this ones are the exact equivalent as Processing converts your code to pure Java.

A basic and important thing to keep in mind is that PApplet extends from java.awt.Component so to your AWT of Swing based app, it will be no different from any other component, and it will be as easy to add to your JFrame or JPanel.

Let’s begin with a simple example

Simple Processing

  public void setup() {
    size(400, 400);
    background(0);
  }
  public void draw() {
    background(0);
    fill(200);
    ellipseMode(CENTER);
    ellipse(mouseX,mouseY,40,40);
  }

Now to add it to our application, we must create a class that extends PApplet, a JFrame to contain it and a Main class to launch it, your app then should look similar to this:

Java Source Code

Main.java

package processJava;
public class Main {
    public static void main(String[] args) {
        new DisplayFrame().setVisible(true);
    }
}

DisplayFrame.java

package processJava;
public class DisplayFrame extends javax.swing.JFrame {
    public DisplayFrame(){
        this.setSize(600, 600); //The window Dimensions
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.JPanel panel = new javax.swing.JPanel();
        panel.setBounds(20, 20, 600, 600);
        processing.core.PApplet sketch = new CircleSketch();
        panel.add(sketch);
        this.add(panel);
        sketch.init(); //this is the function used to start the execution of the sketch
        this.setVisible(true);
    }
}

CircleSketch.java

package processJava;
import processing.core.*;
 
public class CircleSketch extends PApplet {
 
  public void setup() {
    size(400, 400);
    background(0);
  }
  public void draw() {
    background(0);
    fill(200);
    ellipseMode(CENTER);
    ellipse(mouseX,mouseY,40,40);
  }
}

It’s done, now just press run and enjoy.
You now have no excuse to keep your processing sketches apart from your desktop applications.

EDIT: Fixed a small typo on a class’ name

This post is also available in: Spanish