Multiple classes eclipse with processing

Hey!

So I am new to processing and would love some help with how to get multiple classes working in eclipse with processing

this is my code:

This is one class “mouse”:

import processing.core.PApplet;

public class Mouse extends PApplet {
	public void setup() {

		background(0);
	}

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

	public void draw() {
		stroke(255);
		line(0, 0, mouseX, mouseY);

	}
}

And this is the “main class”:

import processing.core.*;

public class Tester extends PApplet {

	public static void main(String[] args) {

		PApplet.main("Tester");

	}

}

I am not sure how to continue from here, it is not like processing where you can have everything in the “main” tab…

1 Like

You should start a bit further back, understanding how processing pre-processes a *.pde is very instructive:-

Processing caches it’s compiled java classes and a .java file every time you run a sketch, Apple will change where in the /var/folders/ directory this is. The fastest way to find the current location is to create a new sketch, don’t save it and press command+k which will open up the subdirectory of /var/folders/ where the sketch is temporarily stored.

Anyway should basically check that you understand what is going on, this will set you in good stead. Basically processing creates inner classes so that they can inherit methods etc from PApplet, you might not want to do that so, you need to know that you can access static methods using PApplet or instance methods on your instance of PApplet.

1 Like

The class containing processings setup, settings, and draw methods should also contain your main method.
All other classes that are not processing sketches don’t need to extend PApplet.

If you want to have those classes use processing methods like “point(x, y)” for example, you will have to pass your main class (“Mouse” in this case)

import processing.core.PApplet;

public class Mouse extends PApplet {

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

    ExampleClass myExampleClass; //Declare instance of your class
	public void setup() 
    {
		background(0);
	}
	public void settings() 
    {
		size(800, 800);
        myExampleClass = new ExampleClass(this); //Initialize instance by passing a reference to your main class 
                                                 //using the "this" keyword.
	}

	public void draw()
     {
		stroke(255);
		line(0, 0, mouseX, mouseY);
        myExampleClass.drawPoint(mouseX, mouseY);
	}
}

and then for example:

import processing.core.PApplet;

public class ExampleClass
{

    private PApplet parent;
    public ExampleClass(PApplet parent) // This will take your main class as an argument to use later on
    {
        this.parent = parent;
    }

    public void drawPoint(int x, int y)
    {
        parent.point(x, y); //Use processing methods like this in other classes.
    }
}

if you haven’t already, take a look at this:
https://processing.org/tutorials/eclipse/

2 Likes

Are you looking to have multiple windows? Then I would use your first code in your first post with some modifications. However, your question is about working with multiple classes. It is possible but you need to be aware how to access the main sketch and only access it if you need to. For instance, if you have a class which is design to sort your data, then this class does not need to have access to the PApplet. Instead, you can pass data to the PApplet. You can do this by instantiating the object within the main sketch and then passing a reference of the PApplet to it as describe by @cumulus in the previous post. However, the most effective way is to return the data to the sketch so it can handle it. What strategy to implement depends on what you want to do and it is the fun part of the designing stage.

Kf

1 Like

Like others have said, you should only have one class that extends PApplet.

Shameless self-promotion:

This guide walks you through using Processing as a Java library.

2 Likes

Thanks for the answers guys, I will try to learn more about processing. thanks for the guide aswell!

THX, help me a lot, very useful