This tutorial assumes you already have Java and Processing installed on your computer. This is also not an Eclipse tutorial, but just covers the basics to get you up and running with Processing in Eclipse.

Step 1. Download and install Eclipse

Grab “Eclipse IDE for Java Developers” from http://www.eclipse.org/downloads/. When you first run eclipse, it will ask you for a workspace directory. This is the directory where you will store your project files so pick something reasonable that you will remember.

Step 2. Create a new project.

If you see a Welcome screen, go ahead and close it. Then go to FILE –> NEW PROJECT and select “Java Project.” Click next. Enter your project name (for example, “TestProcessing”) and select “Finish.”

Step 3. Import the Processing libraries

Eclipse doesn’t know anything about Processing so we’ll have to go and get the Processing libraries ourselves. One way to do this is by pointing Eclipse to Processing’s “lib” directory (see this tutorial), however, because we intend on sharing the project via a CVS, it’s a good idea to copy the necessary Processing files into the project folder itself so that everything stays together. Go to:

FILE –> IMPORT –> GENERAL –> FILE SYSTEM.

Click next. Click browse and find the Processing application. On Windows you will then want to look inside the directory called “lib”, but on Mac (for Processing 1.0=) you’ll have to go inside the application’s “package contents”: Processing->Contents->Resources->Java. Select the file “core.jar” inside the lib (PC) or Java (Mac) folder.

Click “finish.”

You should now see “core.jar” listed under your project in the package explorer.

Right-click on the file and select

BUILD PATH –> ADD TO BUILD PATH.

The “core.jar” file should now appear with a jar icon.

Step 4. Create a class and write your code!

Go to: FILE –> NEW –> CLASS

Enter in a class name “MyProcessingSketch” and click finish. Eclipse may yell at you and say “The use of the default package is discouraged.” We can’t really blame them for getting upset. Packages in Java provide a nice means for organizing your classes and this is a good thing. Nevertheless, for just getting this example working, we can skip thinking about packages.

After selecting finish, you’ll see the following code:

public class MyProcessingSketch {

}

Eclipse is being nice and filling in the class definition for us. Thank you Eclipse! Now, if we want this class to be a Processing sketch, we’ve got to add some code:

  • Import the core Processing library: “import processing.core.*;”
  • Extend PApplet. PApplet is the parent Processing class that allows us to have access to all the Processing goodies.
  • Write setup() and draw() (and specify “public” for these Processing functions).
import processing.core.*;

public class MyProcessingSketch extends PApplet {

  public void setup() {
    size(200,200);
    background(0);
  }

  public void draw() {
    stroke(255);
    if (mousePressed) {
      line(mouseX,mouseY,pmouseX,pmouseY);
    }
  }
}

Step 5. Run!

Go to: RUN –> RUN AS –> JAVA APPLET

For most things, you are often going to want to run your code as a Java Application (rather than applet) and turn on Processing’s present mode. You can do this by adding a main() function to your code.

  public static void main(String args[]) {
    PApplet.main(new String[] { "--present", "MyProcessingSketch" });
  }

Note that the String “MyProcessingSketch” must match the name of your class (and if it is in a package, should include the package, i.e. packagename.MyProcessingSketch).

Once you’ve added the main() function, you can select:

RUN –> RUN AS –> JAVA APPLICATION

Processing in Eclipse with Multiple Classes

Take a look at this example Processing sketch. The example is object-oriented and contains a class called “Stripe.” In Processing, all classes are treated as “inner classes,” meaning they are not individual entities unto themselves, but rather are classes inside of the larger PApplet. This is why you can do anything you can do in a PApplet inside of the Stripe class. You can draw onto the window calling rect() and access the PApplet variables such as width and height. To get this example working in Eclipse, it’s perfectly reasonable to just copy it in as is and include the Stripe class code below setup() and draw() inside the parent PApplet class. However, the inner class methodology can quickly become unwieldy in the case of larger applications with lots and lots of classes.

And so we find ourselves in need of a means for creating separate Java classes in Eclipse that we can use in conjunction with a Processing-based application.

First thing first, we can put all the code that is not the Stripe class in a Java class like with the first example.

import processing.core.*;

public class MyProcessingSketch extends PApplet {
  //	An array of stripes
  Stripe[] stripes = new Stripe[50];

  public void setup() {
    size(200,200);
    // Initialize all "stripes"
    for (int i = 0; i < stripes.length; i++) {
      stripes[i] = new Stripe();
    }
  }

  public void draw() {
    background(100);
    // Move and display all "stripes"
    for (int i = 0; i < stripes.length; i++) {
      stripes[i].move();
      stripes[i].display();
    }
  }
}

Second, we create a new class called Stripe.

FILE –> NEW –> CLASS. Name it “Stripe.” Click “finish.”

If we add the Processing code, Eclipse will give us a bunch of errors.

The errors are all on lines where we call a Processing function (such as rect()) or reference a Processing variable (such as width). This is because a Stripe is not a PApplet and knows nothing about PApplets! We might be tempted to have Stripe extend PApplet, but this would be a fatal error. A Stripe is not a PApplet. A Stripe is a rectangular object that we want to draw onto a PApplet. What PApplet do we want to draw onto? The PApplet that is MyProcessingSketch. Instead of extending PApplet, we simply want to tell a Stripe object about a MyProcessingSketch.

public class Stripe {
  PApplet parent; // The parent PApplet that we will render ourselves onto

In the constructor, we initialize the parent:

  Stripe(PApplet p) {
    parent = p;

And then anytime we want to call a Processing function, we access it via the parent variable.

  // Draw stripe
  void display() {
    parent.fill(255,100);
    parent.noStroke();
    parent.rect(x,0,w,parent.height);
  }

Here’s the whole thing together:


import processing.core.PApplet;

public class Stripe {
  float x;       // horizontal location of stripe
  float speed;   // speed of stripe
  float w;       // width of stripe
  boolean mouse; // state of stripe (mouse is over or not?)
  PApplet parent; // The parent PApplet that we will render ourselves onto

  Stripe(PApplet p) {
    parent = p;
    x = 0;              // All stripes start at 0
    speed = parent.random(1);  // All stripes have a random positive speed
    w = parent.random(10,30);
    mouse = false;
  }

  // Draw stripe
  void display() {
    parent.fill(255,100);
    parent.noStroke();
    parent.rect(x,0,w,parent.height);
  }

  // Move stripe
  void move() {
    x += speed;
    if (x > parent.width+20) x = -20;
  }
}

Finally, the last change we have to make is how we call the constructor. Previously, we had:

  stripes[i] = new Stripe();

But now when we construct a Stripe object, we must pass in a reference to a PApplet. And here, the PApplet is this, a reference to ourselves, the PApplet we are right now, MyProcessingSketch.

  stripes[i] = new Stripe(this);

Here is the full project folder for download.

Another important note. The Processing “color” primitive does not exist in Java. In fact, in Processing a “color” is really just an integer (32 bits with red, green, blue, and alpha components). Processing translates “color” to “int”, but
Eclipse won’t do that for you. So instead of saying:

color pink = color(255,200,200);

you should say:

int pink = color(255,200,200);
  • ender

    Thanx for this man! Your note about how it is wrong to extend PApplet in every new class made me solve a problem I was struggling with for the last two days!

  • http://www.kinrou.com sebastien

    Could you please provide information in how to export an application or an applet when using eclipse.

    I tried to do ‘File | Export’ and then selected the ‘MAC OSX application bundle’ but no success.

    Thanks

  • admin

    This is what you are looking for:
    http://www.rednoise.org/EclipseP5Exporter/

  • http://www.embauchezmoi.com Luc

    Wow !! Thanks a lot !!

  • fire

    You should explain with instructions and picture how you import the data folder in eclipse. thank you

  • Juan

    Hi!
    The tutorial is excellent, but i am trying to move my program from processing to eclipse and i don’t really know how initialize the parent in the constructor when i have arguments in the called, for example:

    class rue {

    int taille;
    int tours;
    vehicule[ ] vehicules;

    //constructor//

    rue(int ttaille,int ttours, int tnumvehicules) {
    tours = ttours;

    vehicules = new vehicule [tnumvehicules] ;

    for (int i=0;i < vehicules.length ; i++) {
    vehicules [i]=new vehicule ( int(random(3)+1),int(random(10)),int(random(1)),int(random(1)));
    }
    }

    ……

    May be you can give me a hand, thanks!

  • Ángel Linares García

    There is some issues with data conversion (float numbers, doubles, etc…) that are getting me crazy…Is nice to have debug functions and prediction typping, but the code looks awfull if you try that everything works.

    Good tutorial :)

  • diatom

    excellent tutorial! I got it working, but couldn’t get eclipse to import opengl libs. I tried substituting the process you suggest for importing the core libs for opengl, and I brought it in and set the path, but it threw errors when running. any suggestions?

  • Anonymous

    i need to update this tutorial. try using proclipsing. it makes all of this much easier!  http://code.google.com/p/proclipsing/

  • Diatom

    Thanks! yes, that definitely made things much easier. I graduated from the opengl libs to toxiclibs, and then ran into the dreaded
    Can’t find
    dependent libraries

     

    Exception in thread “Animation
    Thread” java.lang.UnsatisfiedLinkError:
    C:UsersTomworkspaceRectTree_tx3Dlibbaseopengljogl.dll: Can’t find
    dependent librariesThis has to mean that either the libs are not present or that eclipse cannot see them due to class path issues. my project/lib/base contains opengl which contains
    jogl_cg.dll, jogl_awt.dll, jogl.dll 
    which makes me think that the dependent libraries are in fact where they should be.

    i”m importing them properly in the main class
    import processing.core.*;
    import processing.opengl.*;
    import toxi.processing.*;
    import toxi.geom.*;any suggestions on a bulletproof way to make the libs work?

  • Keny-gomez

    Say I’m in “class3″ and I’m using actionlistener to call “class2″, basically the methods of class2 will open when a button is clicked, instead of putting this in the main method:  PApplet.main(new String[] { “–present”, “class2″ });what do I have to put in the actionlistener body? I’ve been trying to do that and I don’t know how to do it.

  • Rafael

    thank man, you are help me very much

  • Abhishek Mehta

    Hi.. I am using eclipse Indigo and I want to make processing demo’s jar file.. but it doesn’t run and gives errors like following,
    Exception in thread “main” java.lang.NoClassDefFoundError: processing/core/PApplet

    So, please guide step by step to make complete jar file…

    Thank You in Advance

  • Audra Milton-Anderson

    I’m very new at processing, and sometimes my class seems to leave me behind. I have the Learning processing (the beginners guide) Is there another way that I can get this to sink in my head? It’s like if I want something to move I know I start with void draw() but how do I know what to put in it to make it go aside from rect(100,100,50,50)?

  • shiffman

    I have a video series which might help?

    http://icm.shiffman.net/

  • Lucy Zarina Campbell

    My colleagues use Processing 2, is this an acceptable alternative to Eclipse?

  • shiffman
  • Matt Reaves

    This helped so much! I was pulling my hair out trying to draw an ellipse from another class! Thank you!!!

  • jr

    the “public class MyProcessingSketch extends PApplet{” has an error on my project, what’s the problem? :(

  • shiffman

    make sure you import the Processing jars you need. Try https://code.google.com/p/proclipsing/ or ask on forum.processing.org for more help.

  • Andrew Case

    Perfect! We’ve been doing OOP stuff and I wanted to show my students how to have the objects draw themselves without being included as part of the class extending PApplet. This is exactly what I needed. Thanks!