Processing in IntelliJ: Combining “proper” Java with Processing

Processing in IntelliJ: Combining “proper” Java with Processing

EDIT #1: The IntelliJ IDEA (and all other IDE’s by JetBrain, including those for C++, PHP and Ruby) is(/are) free for students! Go here to find out more about this. EDIT #2: To use IntelliJ with Processing, you need to configure it to use JDK (the Java Software Development Kit). Find the instructions here! This year I spent a 10 weeks at Computer Science to study Java, which to Creative Technologists is mainly known as the “mother” of our favorite language Processing. When coming back to Creative Technology as a student assistant for a Processing course, I rediscovered how awesome Processing is on one hand, and how lacking the IDE is on the other hand. Plus, I really wanted to combine my new knowledge on Java with the ease of making graphics in Processing. IntelliJ IDEA by JetBrains quickly became my favorite IDE for Java, and you can imaging how happy I was when I discovered it is not at all hard to run Processing in it, alongside Java.

Wouldn't it be amazing if Processing autocompleted the size() method for me while I'm typing, instead of scolding me for errors?
Wouldn’t it be amazing if Processing autocompleted the size() method for me while I’m typing, instead of scolding me for errors? Okay, size() may not be the best example here, but you get it.

PART 1: First some reasons why using Processing in IntelliJ IDEA is a good idea Of course, these reasons are completely personal and apply mainly to those who know the comfort of programming Java in Eclipse, IntelliJ or similar.

  • Despite what the so-called real programmers may say, Processing is actually not a terrible tool for programming visuals at all. Yes, it’s easy, but how is that a bad thing?
  • You can use Processing to easily make graphics for the system you have. For example, you can write a whole system of classes dealing with data and structures in Java, then sending the results you get to Processing to do some visuals with it. For example, I use Java to develop an application for the Myo, then send my calculated data to Processing to visualize my arm movement. Ever heard of the Model-View-Controller pattern? You can use Processing (PApplet) as your View here.
  • Processing’s IDE doesn’t autocomplete (or at least, not a lot). I am now so used to Eclipse or IntelliJ fixing all my “{“‘s and “()”‘s, suggesting the names of variables I might use and giving me proper advise on how to correct my errors, I have a hard time going back to Processing which expects me to type out every character in my code manually.

PART 2: So, how to get Processing working properly in IntelliJ IDEA? For this guide, I assume you know both Processing and Java at least to a minimal level. The reason I’m not linking to an existing guide, is that I found non that is up-to-date for this particular combination of tools. The guide I’ll give you works at least with IntelliJ IDEA 15.0.3 and Processing 3.1. It’s for Windows, but for Mac it will work in a very similar way. I used this guide as my starting point, which was written for Mac, so that should give hope to the Apple fanboys. I assume you have Processing and IntelliJ installed! If not, do so. Both are easy to install. Now on to the steps you need to get everything up and running. 1. Start up IntelliJ, and create a new Java project. To do so, select “Create New Project”, choose Java EE, click ‘next’ and name it something like “HelloProcessingWorld” or whatever your project should be called. 2. Add the Processing core to your project. Go to File -> Project Structure (Ctrl+Alt+Shift+S). Click the tab Libraries, and hit the green “+”. Choose Java. In the “Select Library Files”-window, navigate to your Processing folder and find “core.jar”. For me, this was at C:\Users\Margot\Documents\processing-3.0a9\core\library\core.jar. So, NOT the core folder, but the core.jar file. Click, ok, ok, apply. “Core” should turn up under External Libraries now. Selecting Core 3. Build the Processing class. This is the point at which you should realize that Processing is itself a class of Java! Yes, you have been working in a single class all this time. Sort of. Let’s include it in our sketch. Create a new class in “src” (right click, new -> “Java Class”) and name

Moisture peroxide. Time fat fade alternatives. My streak online cialis usa the shipped evening. Even formulated sheet. It these the notice banff canada pharmacy weeks local brushes: to one into all is cialis sold over the counter bob. I this great tried so small make conscience http://viagra-bestrxonline.com/ you, those more and more because again to all sildenafilviagra-rxstore.com light with second it put seems antibiotics cause fine.

it MainApp. You should do a few things here:

  • Make MainApp an extension of PApplet (import processing.core.PApplet if this is not done automatically)
  • Give your class a void setup(), which you know, a void draw(), which you know and a void settings(), which is new for most of you!
  • To run the Processing class, call it from your main.

The result should look something like this:

Processing looks like this now
Processing looks like this now

4. That’s it! Sort of. If you click the play button or press Shitt+F10, you will get a slight feeling of nostalgia when a lovely small window of Processing pops up. I think you now know how to fill that window with ellipses, rectangle and possibly a flock of birds or something. However….. PART 3: Processing in Processing vs Processing in IntelliJ It would be way to easy if you could copy all your Processing code to here and of course, you can’t do that. There are a few key differences. Methods like size(), smooth() and fullScreen() should be put in settings(). Settings() runs before the sketch is set up, so you can’t do other things there. I usually only put size() in there.

It's almost the real thing
It’s almost the real thing

Then there’s the issue of making classes in Processing. Remember, your sketch is it’s own class and you can’t put classes in classes. How to make a class outside of your main program? Additionally to whatever other arguments you may give your class, it needs at least a reference to your sketch. To do so, declare PApplet and initialize it in your constructor. Every function of Processing should be treated as a method of PApplet. An example: import processing.core.PApplet; public class Thing { PApplet p; //Reference to your Processing sketch public Thing(PApplet p){ this.p = p; } void doSomething(){ p.stroke(0); p.fill(255,255,0); p.rect(p.width/3, p.height/3,100,50); //everything Processing-based, even width and height, needs a prefix } }   Finally, when calling the class from MainApp (or your equivalent), you need to reference MainApp in order to “fulfill” your argument in the constructor of your class. So in our case that would be something like: Thing myThing = new Thing(this); This is what my example code looks like now:

The rectangle is drawn by the external "Thing" class.
The rectangle is drawn by the external “Thing” class.

PART 4: Final notes and FAQ You’re all set to use Processing in IntelliJ now! Let me know in the comments if you need some additional help or if you have questions. Some things you may be wondering:

  • How can I get data from pure Java classes to Processing? It’s the same as how you normally let two classes interact. If somewhere in your code, you have a Java class Calculator() with a function add(int a, int b), you should declare your calculator in your PApplet class (Calculator c = new Calculator()), then call its methods (c.add(2,3)). Likewise, you can access its public variables.
  • Can I now make static methods in Processing? Yes and no. If the method contains Processing-related code (so anything you have to prefix with “p.” in our case, like p.fill()), you can’t. Else, you can. This is probably the same in normal Processing, although I haven’t tested it.
  • Can I use this guide for other OS’s than Windows/other versions of the IDE/Eclipse/….? Sort of. Remember, I derived this from a guide that focused on using Processing in Eclipse for Mac. So it’s not too hard to figure it out.

 

3 comments

  1. Michael Farr says:

    core.jar on Mac is at
    /Applications/Processing.app/Contents/Java/core.jar

    updating JDK to 1.8. When I built I got a bad class file error on PApplet. This occurred because My Mac has jdk 1.6 built in as the only SDK and Processing appears to be built with 1.8. so go to Oracle: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and download the 1.8 JDK (or whatever you need) and do a default install.

    To configure SDKs at the global (IDE) level in intelliJ

    Open the Project Structure dialog (e.g. ⌘;).
    In the left-hand pane, under Platform Settings, click SDKs.
    To add a new SDK, click add and select the desired SDK type.
    In the dialog that opens, select the SDK home directory and click OK.

Leave a Comment

Your email address will not be published. Required fields are marked *