How to use Processing 4 with Java and Gradle

How to use Processing 4 with Java and Gradle

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-1024x651 How to use Processing 4 with Java and Gradle

Clone the repository locally:

Bash
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:

processing4-java-navigation How to use Processing 4 with Java and Gradle


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

Java
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.

Bash
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 How to use Processing 4 with Java and Gradle

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:

Leave a Reply

Your email address will not be published. Required fields are marked *