Monday 29 March 2010

Processing in Netbeans

If you've ever browsed the Processing learning section you may have seen the article showing how to use Processing from within Eclipse.
How, I'm more of a Netbeans fan myself. This isn't a criticism of Eclipse, which I also use extensively. I just like Netbeans. Therefore, I'd like to share how to get Processing working from within the Netbeans IDE. I'll assume that you already have an installation of the Processing IDE. This howto is based almost directly on the Eclipse tutorial, so differences should be obvious.

Step 1. Download and install Netbeans

Netbeans can be downloaded from netbeans.org. Any of the bundles that contains Java SE will do. I use the 'All' bundle but you can get away with a smaller one.

Step 2. Create a new project

Ctrl-Shift-N creates a new project. For the project type, select Java >> Java Application. On the next page of the New Project dialog, give your project a name; I used 'ProcessingTest'. Also tick 'Create Main class' and enter a new like 'uk.co.mooduino.processingtest.Main'. You should use your own domain (this style of using a backward domain to create a unique namespace is common within java).

Step 3. Import the Processing library

To tell Netbeans about how Processing works, we need to import the Processing core library. Expand the 'ProcessingTest' project and then expend 'Libraries'. There should be one entry already, for the JDK. Right-click on the Libraries icon and select 'Add JAR/Folder...'. Browse wo where you installed the Processing IDE and then descend into the lib folder. Select 'core.jar'.

Step 4. Create a class and write your code!

Ctrl-N opens the New File dialog. Select 'Java Class' and give the class a name like 'MyProcessingSketch'. I'm going to use the same code as the Eclipse example;
package uk.co.mooduino.processingtest;

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!

Before we can run this code, we need to modify the Main class that Netbeans already created. Modify the code as follows:
package uk.co.mooduino.processingtest;

import processing.core.PApplet;

public class Main {

    public static void main(String[] args) {
        PApplet.main(new String[] {"--present", "uk.co.mooduino.processingtest.MyProcessingSketch"});
    }
}
Press F6 to execute the code. Your screen should switch to Processing running in fullscreen mode and the middle section should allow you to draw on it.



5 comments:

  1. Thank you for posting this tutorial, it was very helpful.

    TonyA

    ReplyDelete
  2. Thank you very much, greetings from Colombia!

    ReplyDelete
  3. thank you, very helpful!

    ReplyDelete
  4. Thank you very much. This tutorial was very helpful to me.

    ReplyDelete
  5. good job finally worked all the other tutorials were lame!

    ReplyDelete