[Eclipse] Use PApplet methods (e.g. 'fill()') in another class

Hey! I am still pretty unexperienced with classes and everything so I would like to know why I cant call methods like ellipse() or fill() in a class other then 'UsingProcessing' (I did everything like in this tutorial explained).

The method ellipse() is undefined for the type TestClass

I first thought like I need to import processing.core.PApplet; but that doesn't seem to work for me :/

regards, TPRammus

EDIT: Thanks to the answers, I edited my code. Now I fist pass "this" to a static (PApplet) variable "PA" in my class. And I can use "PA" like this: PA.ellipse(1, 2, 3, 4);

Answers

  • edited March 2017 Answer ✓

    Non-inner (top) classes need to request the PApplet reference it wants to access.

    Generally that is asked right in the class' constructor like this:

    import processing.core.PApplet;
    
    public class NonInner {
      final PApplet p;
    
      public NonInner(final PApplet pa) {
        p = pa;
      }
    }
    
  • edited March 2017

    Thanks for your answer! So everytime I want to create an object I will need to pass the PApplet reference to it? How would I pass the PApplet to an object? I dont see anything I could pass.

  • Inside whichever class is a subclass of PApplet, you can use the syntax NonInner ni = new NonInner(this);. Learn about inheritance in Java for more information.

  • edited March 2017

    Just some small clarifications: Inner classes aren't subclasses of their enclosing outer class! L-)

    Even though they can access every non-static member of its enclosing classes. :P

    According to https://en.Wikipedia.org/wiki/Inner_class: :-B

    ... an inner class or nested class is a class declared entirely within the body of another class or interface. It is distinguished from a subclass.

    In order for any class to be considered an actual subclass of another, we gotta use keyword extends (or implements for interfaces).

    Thus, if we wish for some nested class to become a subclass as well, we apply extends to it: :ar!

    void setup() {
      println(PApplet.class.isAssignableFrom(VanillaInnerClass.class)); // false
    
      println(PApplet.class.isAssignableFrom(PAppletInnerSubclass.class)); // true
      println(new PAppletInnerSubclass() instanceof PApplet); // true
    
      exit();
    }
    
    class VanillaInnerClass {
    }
    
    class PAppletInnerSubclass extends PApplet {
    }
    
Sign In or Register to comment.