Introduction to Processing (Java)

Lesson 1 of 14 · Processing

Processing is an environment made as an add-on for programming animations and visual applications, with good and easy interaction with the user.

Processing is a flexible software framework and development environment, designed mainly to make visual artistic expression through programming easier. Its purpose is to bring programming closer to artists, designers and others without previous coding experience.

It is written in Java and can be seen as an add-on that provides support, i.e. an API with functions for creating animations and visual applications. It is based on writing sketches (sketch).

Processing is open source.

It has its own environment, PDE (Processing Development Environment), where you can choose the language you will write code in. It can also be connected with Eclipse IDE or another Java development tool (IDE), by adding the libraries that contain Processing to the existing Java libraries. At the time this tutorial was written, Processing 4 was used

The API consists of several libraries that, besides the basic functions, support: sending data over a network, reading images from a camera, saving images in pdf format.

Processing was originally designed so that, with its simple syntax and many options for creating graphics, images and animations, it would be easier for developers to build creative animated and interactive applications.

It is still used in server and desktop applications, but if you need to put an animation into a website, it is better to use the JavaScript library for Processing, p5js.

Up to version 3, it was possible to use the Java version of Processing on the web too, but you had to add the processing.js script to the site. Development and support for this script have stopped, and it does not support Processing versions from 3 onward. So if you create a web application as .pde and want to put it into a web page, you first have to rewrite the code you wrote in, e.g., Processing 4 before uploading it to the website, because the new functions are not supported, and then also upload the processing.js library, which translates the code written in Java into JavaScript code. That is why, if you are already making a website and want to enrich it with an animation made in Processing, it is smarter to use the p5js library. Read more about this in the article: happycoding.io/tutorials/p5js/which-processing

Features of Processing

1.

A simple environment: Processing offers a simple development environment that lets you start working quickly. Its interface is minimal and focused on making creative expression easier.

2.

Syntax: Processing uses a simple syntax that is easy to understand, even for those who are new to programming. However, under the hood, Processing relies on Java, which means that users have the full power of Java programming available when they need it.

3.

Graphics and interactivity: Processing is specially designed for drawing graphics and creating interactive visualizations. It has built-in functions for drawing shapes, handling colors, animations and working with images.

4.

Community and resources: There are many resources, including tutorials, books and an online community that gives support and shares code examples.

The programming environment and the language

1. The programming environment: Processing provides an integrated development environment (IDE) that is simple and meant for beginners. This environment lets you write, compile and run code in one interface. The Processing IDE includes tools for drawing, animation and interactivity, which makes it ideal for visual projects.

2. The language: Although Processing is based on Java, it uses a simplified syntax adapted to beginners and people who work in the visual arts. Code written in Processing is translated into Java, which makes it easy to move between Processing and Java.

A library for Java

Library: Processing is also a Java library that can be used inside Java projects. This means you can use Processing's features in standard Java applications, combining Processing's graphics with other Java libraries and tools.

How Processing and Java are combined

  • Based on Java: Processing is based on Java, which means that Processing users can use all the features of the Java language. This combines the simplicity of Processing with the power and flexibility of Java.
  • Using Java libraries: In Processing you can use any Java library, which extends the features Processing offers.
  • Learning: Processing can be a great introduction to programming in Java, especially for visual learners and creative people, because it gives visual results quickly.

Conclusion

Processing is a great tool for artists and designers who want to explore the world of programming through visual projects, while Java gives a solid foundation for developing all kinds of software applications. Combining the two can give a powerful environment for creative and technical development.

The micro:bit workshops are part of the creative programming course organized by the Regional Center for Talents "Mihajlo Pupin" in Pančevo:
Creative programming (in Serbian)

Installing Processing

It can be downloaded from the site:

processing.org/download

The PDE environment

Start Processing 4 by double-clicking its icon, and the following environment will open:

Starting the PDE for Processing
Figure 1: Starting the PDE for Processing

If you are a beginner, you can choose one of the offered examples and then change the existing code, or start an application from the beginning. To program in Processing you need basic knowledge of the JAVA programming language or of one of the supported ones: JavaScript, Python etc. This tutorial is based on the Java programming language.

Organizing code in Processing

The files in which code is written are actually sketches, and they are kept in a folder called the sketchbook. This folder can be changed in the PDE by clicking

File->Sketchbook

This can be seen in the following picture:

Processing Integrated Environment
Figure 2: Processing Integrated Environment

An application can be exported by clicking

File -> Export Application

From Java to Processing

If you know the basics of programming in JAVA, as well as the basics of object-oriented programming – classes, objects, inheritance etc. – then, assuming the needed Processing libraries have been added, the code in the Eclipse IDE would look, for example, like the following picture:

The main class in Eclipse, part 1
Figure 3: The main class in Eclipse, part 1

At the start of the main class, to program in Processing, you need to import the basic library:

import processing.core.*;

This is only the basic library; specific ones are added when needed.

So the starting class must inherit the PApplet class. Also note the overridden methods from the PApplet class: settings, setup (in figure 3) and draw (see figure 4).

The main class in Eclipse, part 2
Figure 4: The main class in Eclipse, part 2

In the navigator on the right you can see the headers of these methods, and clicking a header puts the cursor on that method.

Coding a sketch in the PDE environment is different, because in the background the basic libraries are already imported, the header line of the main class is written, and the methods setup and draw are inserted for overriding,

So you do not need to write, e.g., “class Glavna” (main class), and you do not need to write “import processing.core.*;” (see figure 2).

The methods settings, setup and draw from the PApplet class

Animations are made by first creating objects and setting them to their starting state, and then changing the state of the objects through frames at a certain time interval, e.g. 0.05 s. So we need, on one side, methods that create the objects in their starting state and are called only when the application starts, and on the other side a method that changes these states and is called periodically, according to the defined time interval (0.05 s in the example). The methods settings and setup belong to the first group: they define the starting state and are called only once, while the method draw is called every dt seconds.

This method takes care of the needed changes of the state of objects or variables, and also of drawing these objects on the screen.

The example in the pictures shows balls moving randomly, and we can see that these methods are used here for the following:

  • settings: Sets the display size to full screen
  • setup: Creates an array of balls at random
  • draw: Moves and draws the balls

Introduction to Processing – video

A program in the PDE environment

In the PDE environment we would put this same example inside a new sketch. When a new sketch is opened, we rename it, for example, to BouncesGame (see the picture). The example shows how to simulate the chaotic motion of balls in Processing.

Creating a new sketch in Processing
Figure 5: Creating a new sketch in Processing

Here we type the code that belongs to the main class, which will be called “BouncesGame”, and after running, a folder with the same name will be created:

The starting folder of a Processing project
Figure 6: The starting folder of a Processing project

The starting class is a bit different from the starting class developed in the Eclipse IDE environment:

Code from figure 7: The starting file in Processing – BouncesGame

Bounce[] bounces;

@Override
public void settings() {
  fullScreen();
}

@Override
public void setup() {
  bounces = new Bounce[100];
  for (int i = 0; i < 100; i++) {
    float x = (float) (width * Math.random());
    float y = (float) (height * Math.random());
    Bounce b = new Bounce(x, y, 5);
    bounces[i] = b;
  }
}

@Override
public void draw() {
  this.background(color(200));
  // completed – the rest of the method was not visible in the picture:
  for (Bounce b : bounces) {
    b.move(width, height);
    ellipse(b.center.x, b.center.y, 2 * b.r, 2 * b.r);
  }
}
Note: The picture showed only the start of the method draw(); the loop that moves and draws the balls has been added (marked with a comment).

Here there is no class header and no import of the basic library; instead there are variable declarations and the methods settings, setup and draw just like in the Eclipse version.

The other class, which describes a ball (Bounce), is written the same way as in the Eclipse IDE environment, as you can see in the following picture:

Code from figure 7: Example in Processing: BouncesGame – the Bounce class

import processing.core.PVector;

public class Bounce {
  float r;
  PVector center;
  PVector speed;

  public Bounce(float x, float y, float r) {
    super();
    center = new PVector(x, y);
    speed = new PVector((float) (10 * Math.random()), (float) (10 * Math.random()));
    this.r = r;
  }

  public Bounce() {
    super();
  }

  void move(double w, double h) {
    center.x += speed.x;
    center.y += speed.y;
    if (center.x < 0 || center.x > w) {
      speed.x *= -1;
    }
    if (center.y < 0 || center.y > h) {
      speed.y *= -1;
    }
  }
}

After running, you will see the balls moving chaotically on the screen:

Example in Processing: BouncesGame – running the program
Figure 8: Example in Processing: BouncesGame – running the program

Check your knowledge

Open the quiz

Quiz: Introduction to Processing

1. What is Processing?




2. How many times does the function setup() run?




3. What does draw() do?




4. What is the development environment of Processing called?