From the Lab
Using Processing in Eclipse
Gail Carmichael — February 10, 2011 - 1:47pm
A lot of us in the lab use Processing for a variety of things, such as prototyping game ideas. While the little IDE Processing comes with is ideal for those just getting started with programming, it's a bit too simplistic for many of us. Luckily, it's possible to use all the Processing library has to offer in a regular Java project.
Here are my notes on how I set up Eclipse to work with Processing and SVN.
Setting up SVN
I like to use Subclipse. The instructions on how to set that up are available on the Subclipse website.
There's a bit of extra work in getting this JavaHL thing, so be sure to not ignore the information at the top of the page (follow the link to the wiki they mention).
Start a New Project
Note: You may want to create your project using the subclipsing plugin described below ("Exporting to an Applet Like Processing Does"), but you don't have to - you can still use the plugin to export if you create the project as per this section.
Start up Eclipse with the workspace you want to use.
Create a new Java project naming it whatever you want.
Right click the Java project, go to Team > Share Project. Choose SVN. Create a new repository location if yours isn't there yet. Otherwise just select it.
When choosing the repository location, choose the folder that the new project folder will go into. Eclipse will make a new folder for you, so don't worry about creating it with an SVN repository browser ahead of time.
Getting Processing Into the Project
These basics are from the more detailed article on Processing's website. I am assuming you have installed Processing on your computer already.
To import the Processing library, right click your project and say Import. Choose General > File System.
Browse to the location of your Processing installation folder and find the lib folder (Windows) or Java (Mac).
Choose the core.jar in the list of jars and click finish.
The jar file is now physically in your project. You will probably want to include it in your SVN repository to ensure everything is complete. To do this, right click, say Team > Add to Version Control. Now when you commit your project it will be included.
Running as an Applet
As per the same detailed article linked to above, we do the following to use Processing in our project:
- 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).
The best way to run is to add the following to the main method and to run the program as an applet:
PApplet.main(new String[] { /*"--present",*/ " mainPackage.MainClass" });
You can uncomment the --present part to run in full screen mode.
Differences from Processing IDE
In the Processing IDE, you always have access to the PApplet members and methods. This is because each tab you create adds an inner class, and each class within the tabs is an inner class to that. So everything ends up inside a PApplet.
In Eclipse, you wouldn't set it up this way. Your main class that extends PApplet is like what you get in the Processing IDE. But the rest of your classes won't extend PApplet as well, since you really only want one applet. Instead, what you can do is pass the PApplet class to the other classes you create so they can access the applet's members and methods (almost like in the IDE, except now you go through the reference to the applet that you stored).
I like to create a new class for each object that will appear on the screen, and give each of these objects its own draw method (as well as any others needed such as for handling mouse clicks). Then in the main PApplet's draw method, I go through each object I've created and call its draw method. Since each object has a reference to the applet, it can make use of the usual Processing functionalities to do the actual drawing. This keeps everything organized in a nice way.
A final thing to note is how to use fonts with the Processing text() commands. You can open up the usual Processing IDE and create a font there. After saving the placeholder sketch, go find the data folder, and copy that into your bin folder for your Eclipse project. Then you use the font as you normally would. If you are using SVN you'll probably want to add that data folder so you will always have the font from there on in.
Exporting to an Applet like Processing Does
That whole one-click export thing that the Processing IDE does is pretty handy. Luckily, you can get the same thing in Eclipse with proclipsing. Check out their site for instructions on installing and setting up their plugin.
If you want to use the export, you will have to use numerical values in the size() function of your PApplet class's setup(). Otherwise it doesn't know how big to make the window.
Happy processing!