Using Libraries with the Processing Programming Language

Figure 1: Including a Processing Library - Mark Alexander Bain
Figure 1: Including a Processing Library - Mark Alexander Bain
A simple tutorial showing how to include libraries in a Processing application and how to create functioning buttons that the users can press

Any programmer can create visual applications quickly and easily with the Processing programming language. Even a novice programmer can soon learn how to start making Processing Programming language applications.

However, the functionality of an application need not be limited by the skills and experience of an individual programmer. The programmer just needs to know that there are a number of Processing libraries already in existence that may aid them in their project.

A Simple Processing Programming Language Application

It is very easy to create a simple drawing application with Processing:

int x = 0;

int y = 0;

void setup() {

size(480, 480);

background(255, 0, 0);

stroke(0, 255, 0);

}

void draw() {

if (mousePressed) {

line(x, y, mouseX, mouseY);

}

x = mouseX;

y = mouseY;

saveFrame("/tmp/output.png");

}

This works very effectively but with just one drawback. The output file is saved every time that the user moves the mouse pointer over the application. The obvious solution is to add a button that will allow the user to save the file themselves. That's not built into Processing by default, but it is possible to add a library that will do the job.

Obtaining and Installing New Processing Libraries

There are a number of libraries that come packaged with Processing and more that can be downloaded from the Processing libraries page. Each of the libraries is in the form of a ZIP file which needs to be downloaded and unzipped into Processing's library folder on the developer's computer (on Linux that will be something like ~/processing-1.1/libraries). Once the library has been downloaded and unzipped into the right location then the programmer can restart Processing and the new library will be available.

Including a New Processing Libraries

Fortunately a Processing programmer does not have to remember which libraries are available to them. There is a menu to do the job. To select and use a library the programmer needs to click on:

  • Sketch
  • Import Library..

The required library will then be included in the project (as shown in figure 1 at the bottom of this article).

Using Libraries in Processing Projects

The project should now contain a line something like:

import controlP5.*;

In this case Andreas Schlegel's custom GUI elements library has been imported.

Programming in Processing with Libraries

There are a number of libraries that can be used for a project. One example is the Interfascia graphical user interface library:

import interfascia.*;

GUIController c;

IFButton save;

int x = 0;

int y = 0;

With the required components declared they can be added to the main screen in the setup function:

void setup() {

size(480, 480);

background(255, 0, 0);

stroke(0, 255, 0);

c = new GUIController (this);

save = new IFButton ("Save", 0, 0, 40, 20);

save.addActionListener(this);

c.add (save);

}

The original draw function can be used, but an additional function is required (as shown in figure 2). This will fire when the button is pressed:

void actionPerformed (GUIEvent e) {

saveFrame("/tmp/output.png");

}

If the user now runs the application then they will be able to draw on the application, using the minimum of processing power, and then save the image when they are ready. The programmer can then, of course, produce a professional application with the minimum amount of effort.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement