astronaut

How to use Processing 4 with Java and Gradle - Step by Step Tutorial

How to use Processing 4 with Java and Gradle - Step by Step Tutorial

August 30, 2024 2 min read
Java + Processing

Processing 🔗 has been a favorite in the world of creative coding, traditionally using the Java programming language. While there are modern alternatives like Kotlin, Java remains a powerful and widely-adopted choice.

In this guide, we’ll walk through the steps to set up a Processing project with Java and Gradle.

Note: This blog post is meant for people looking how to integrate processing4 with Java. Look here 🔗 for a Kotlin gradle guilde.

Prerequisites:

  • You should have Gradle installed on your machine.

Getting Started

1. Cloning the Template from GitHub

First use the template and create a new repository based of the https://github.com/Duckulus/ProcessingGradleJava 🔗.

Processing Java Template Repository

Clone the repository locally:

git clone https://github.com/<YOUR USERNAME>/ProcessingGradleJava.git
cd ProcessingGradleJava

Replace <YOUR USERNAME> with your GitHub username. If you have renamed the repository, make sure to update it inside the command as well.

2. Editing the Sketch File

Navigate to src/main/java/org/example/Processing.java with your favorite editor. You should find the file with the following code:

Processing Java Navigation View

Start by adding your Processing logic. For instance, to draw a moving ellipse:

package org.example;

import processing.core.PApplet;

public class Processing extends PApplet {
    private float x = 0;

    public static void main(String[] args) {
        PApplet.main("org.example.Processing");
    }

    public void settings() {
        size(800, 600);
    }

    public void setup() {
        background(200);
    }

    public void draw() {
        background(200);
        fill(255, 0, 0);
        ellipse(x, height / 2.0f, 50, 50);
        x += 5;
        if (x > width) x = 0;
    }
}

3. Running the Sketch with Gradle

After you’ve edited your sketch, it’s time to run it. Inside the root directory execute the following command.

gradle run

Gradle will compile and run the sketch. You should see a window pop up displaying the Processing canvas with your visualizations.

Processing Ball Example

Conclusion

Java and Processing have a long-standing relationship in the realm of creative coding. Integrating them with Gradle allows developers to manage dependencies and build projects efficiently. Dive into the world of visual art with Java, Processing, and Gradle and bring your ideas to life.

Share this content: