7

I downloaded processing from http://processing.org. How is it possible to use porcessing in my Java application. I want drawing stuff depending on the logic in my Java application. To avoid the drawing in Java I want to use processing instead.

5
  • There are some simple tutorials. Have a look at them. processing.org/tutorials
    – manya
    Jan 23, 2014 at 13:45
  • processing.org/reference/environment/#Programming_modes This looks like covering Processing in Java Jan 23, 2014 at 13:49
  • I included the core.jar from pocessing libs folder Jan 23, 2014 at 14:00
  • only the last tutorial is maybe useful, but the example is not working on the call of the size() methode id get a Null Pointer Exception Jan 23, 2014 at 14:04
  • Fount the way to include. 1. Use core.jar from processing/core/library 2. Import processing.core.PApplet; 3. Create AnClass extending PApplet (use Processing functions in) 4. Create new Applet in your application > PApplet.main(new String[] { "--present", "MyProcessingSketch" }); Jan 23, 2014 at 17:49

4 Answers 4

8

Piece of cake,

First,

In your IDE (eg Netbeans) first include the processing jar in your build, put it some place your program can find it. For example if you use maven just add the dependancy:

<dependency>
    <groupId>org.processing</groupId>
    <artifactId>org.processing.core</artifactId>
    <version>2.1.1</version>
</dependency>

Second,

Add a main class to your program this can be very simple. You just need to reference the class where your code will be:

public class Application {

    public static void main(String[] args) {
        new Application();
    }

    public Application() {
        init();
    }

    private void init() {
        Visualization.main("me.qcarver.ballsack.Visualization");

    }
}

Lastly,

Add your new class with the package name as you gave in quotes above. The only thing to remember is this class must (1) import processing.core.PApplet (2) extend PApplet (3) implement public void draw and public void setup

Eg:

   package me.qcarver.ballsack
   public class Visualization extends PApplet{

    public void setup() {
        size(500,400);
        background(grayValue);        
    }

        public void draw(){
            elipse(200,200,50,50);
        }
    }

Snippets above are based on this example project which runs Processing.org code in a java application.

3
  • 1
    I know we aren't supposed to use this area for this but 'ballsack' had me rolling. Thanks for this hidden gem. May 18, 2016 at 4:54
  • If anyone has trouble with this code and the latest Processing libraries - I've updated for v3.2.1 in a separate answer. Oct 23, 2016 at 11:28
  • 1
    This is missing a few details: Visualization needs to be a toplevel class or static inner class (so that it has a no-args constructor -- this won't work if Visualization is a non-static inner class), and the correct syntax for calling the PApplet is Visualization.main(new String[]{Visualization.class.getName()});, i.e. it needs to be wrapped in new String[]{}. Also, (not directly related to the question, but) if calling size(xsize, ysize, PDF, filename), this will cause the PDF file to be written an infinite number of times -- you need to disable frame refresh when writing PDFs. Feb 2, 2018 at 18:06
4

If anyone has trouble with the earlier code example and the latest Processing libraries - I've updated for v3.2.1 and uploaded the working code:

https://github.com/thomasbratt/ProcessingInIntellij

package com.github.thomasbratt.processingtest;

import processing.core.PApplet;

public class Visualization extends PApplet {

    public static void main(String[] args) {
    PApplet.main("com.github.thomasbratt.processingtest.Visualization");
    }

    @Override
    public void settings() {
        size(640, 480);
    }

    @Override
    public void setup() {
        fill(120,50,240);
    }

    @Override
    public void draw(){
        ellipse(width/2,height/2,second(),second());
    }
}

pom.xml:

<dependency>
    <groupId>org.processing</groupId>
    <artifactId>core</artifactId>
    <version>3.2.1</version>
</dependency>
1

Turns Out When You Download processing in core/library There Is Core.jar If You Add That To Your Class Path You Can Do It Here Is A Skeleton For You To Use:

package com.company;

import processing.core.PApplet;

public class Main/* "Main" Can Be Anything */ extends PApplet{// Enable Processing Features Here

    public static void main(String[] args) {
        PApplet.main("com.company.Main");// Run The App
    }

    public void settings() {// This Actualy Would Be setup() in processing IDE
        size(500,400);
    }

    public void draw(){// The Draw Function
        ellipse(200,200,50,50);//All Processing Functions And Constants Can Be Used Here
    }
}

If You Want To Use Processing Functions Like mouseClicked Use This Skeleton In The Class:

public void RANDOM_PROCESSING_FUNCTION(){
       // Stuff You Want To Do
}
1
  • By The Way This Is if You Dont Have Something Like Maven Nov 24, 2021 at 0:26
-2

I'd suggest you learn how to do your drawing in Java, see for example: http://docs.oracle.com/javase/tutorial/2d/ Using processing in java is not really going to make it simple.

1
  • 3
    Using Processing in Java is great! So much easier than Java for drawing, and so many examples to learn from. Feb 12, 2018 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.