Can I create new class which is not a inner class but still with the ability to draw shape?

If we create many class with different function, but all need to draw their shapes. How can we split the new class from the one extends PApplet and still keep the ability to call it’s build in methods?

It’s too long to manage the class if they are all inner classes.

1 Like

Inner classes aren’t subclasses that extend their outer class but merely just another member of it, along w/ its fields, methods & constructors.

B/c of that an inner class has access to any other member of its outer class regardless its access level being private or anything else.

Now if you wanna make your class not depend on PApplet inside a “.pde” file you can declare it as static.

Or you can create a “.java” file tab which would make your class 100% self-standing.

Regardless which way you pick you’ll need to request (or find out) a PApplet reference in order to access the Processing API inside your non-inner class.

2 Likes

If you do the tab name must be the same name as the class (including case). If you want to access Procesing’s methods you need to add an import to this tab and a reference to the PApplet (sketch) object
If I remember correctly to make a class called Foo create a tab called Foo,java and inside

Foo,java

import processing.core.*;

public class Foo{
  PApplet p; // reference to the sketch
  
  public Foo(Papplet papp){
    this.p = papp; 
  }

  drawRect(){
    p.fill(255,0,0);
    p.noStroke();
    p.rect(20,20,200,50);
  }
}

In you main sketch tab you can create a Foo object like this

foo = new Foo(this);

and in draw something like this

foo.drawRect();

If I made a typo I am sure you can work it out.

PS welcome to the forum

1 Like

That’s a requirement only if there’s 1 class (or interface) declared as public.
Otherwise the “.java” file can have a different name.

1 Like

Thanks, so we need keep PApplet reference where need the Processing API. :ok_hand:

Thanks, I use Processing with vscode, this is the way I need.