How to integrate external library with processing

Hi, I'm writing a library I intend to use with processing at some point. I'm writing the classes in IntelliJ, and (as expected) I get errors when trying to call any of Processing's draw methods and constants. I can ignore the errors for now, but if I do call, say, rect() inside my library, when I import the library in processing, will it still work? Or do I need to call something like processing.core.rect()?

I know Processing itself runs the app from a class, so I'm also curious what the IDE actually does with an import statement given that you can't import within a class.




  • PApplet methods like rect() act upon a PGraphics "canvas".
    Therefore your library needs a fully working PApplet's instance reference in order to access its canvas.

  • So will I actually need to add the source files for PApplet? And then do I have to create an instance of it, or do I need to extend it?

  • I want to clarify, I don't intend to run processing from IntelliJ. I'm just making a library that I will import into Processing and use in the Processing IDE.

  • edited April 2017 Answer ✓

    So will I actually need to add the source files for PApplet?

    Got no experience on that yet. AFAIK, you're gonna need to have "core.jar" in your classpath.

    And then do I have to create an instance of it, ...

    No. It's the user of your library which gotta do that.
    Your library merely requests that PApplet reference from the user.
    Preferably, right on the constructor.

    ... or do I need to extend it?

    Don't do it. At most, if you need to use some Processing's constants in your class, you can implements PConstants interface there. O:-)

  • edited April 2017

    Why don't you study some existing 3rd-party libraries for Processing to have some idea how it's done: ~O)

  • Yes, your constructor needs to accept a reference to a PApplet. Also note that you need to store a reference to it in your class, and use it to draw stuff to the canvas. And drawing stuff should always occur in draw() of PApplet.

  • edited April 2017

    I trust that you know where the core.jar file of Processing exists.

    If you use Eclipse, there already are tutorials on how to use that.

  • @CantSayIHave --

    1. Have you read the Library Overview and Library Basics?
    2. As per Library Basics, are you basing your setup on the Processing Library Template?
    3. As per the template, did you "Locate and add Processing's core.jar to your build path" in IntelliJ?

    If it is missing, importing core.jar in the correct location should prevent your development environment from spewing errors.

  • Yes, if you have no prior experience in creating a library, the processing library template can help you a lot.

  • Yes, if you have no prior experience in creating a library, the processing library template can help you a lot.

    But that's mainly for Eclipse, not IntelliJ. Nobody should be using Eclipse when they can use IntelliJ.

    I have managed to create libraries with IntelliJ. If OP has any specific questions I can help. I dont' have time to make a full blown tutorial at this moment but if there's enough interest I can make a detailed step-through or even make a tutorial video.

    In short, use ctrl+alt+shift+s to go to project structure, go to libraries, import core.jar from the Processing install location (and all the other .jar files in that folder if you want to use the OpenGL renderer). Then just start writing your classes. In your main class you can use the registerMethod() way described in the Library Basics page to have a method be able to draw on the P5 canvas. (Personally, I prefer to write methods that return a PGraphics object that I can retrieve and display in the sketch).

  • You're certainly right, @colouredmirrorball, IntelliJ IDEA is much better than Eclipse. But for simple purposes, Eclipse will suffice.

    But how did you create a PGraphics in the first place? You used the constructor, right (createGraphics() most certainly shouldn't work)? But is that even possible when using OpenGL? For example, if the user uses the default renderer, and your function requires P3D, how do you do that?

  • (createGraphics() most certainly shouldn't work)

    As long as a class got a working PApplet reference, all Processing's API is available to it.

    Of course, for static API members, there's no need for PApplet reference. ~O)

  • @GoToLoop Are you sure? Whenever I try p.createGraphics(), the library doesn't work.

  • (And I've made no stupid mistake like a wrong reference to PApplet)

  • edited April 2017

    There's a method to check which renderer the sketch uses. You need that in the argument of createGraphics(). When your library is requiring a specific renderer you can check for it in the constructor of your main class and throw an error when it's the wrong one.

    That's all there is to speculate about with the information given ;)

  • In IntelliJ Idea you can import the Eclipse project. I can edit and compile my library both in Eclipse and Idea:

  • I see. So, like what I meant to say, you can only use the same renderer in the createGraphics() as the main sketch.

Sign In or Register to comment.