Eclipse Processing Classes

Hello everyone. I am here with yet another problem, hoping for your help. I've recently started using Eclipse for my Processing project because the number of classes started exceeding the available tabs in Processing app.

So I've got a question about classes. Let's say I have an object of another class created inside my main class, also there is a boolean variable:

import processing.core.PApplet;
    public class MainClass extends PApplet {
            MyClass obj;
            boolean variable;

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

        }

        public void settings() {
            size ( 1280, 720);
        }

        public void setup() {
             obj = new MyClass(this);
        }

        public void draw() {

        }
    }

Then there is another class which, if it was created in Processing app, would see both "obj" and "variable", but in Eclipse it doesn't recognise them:

import processing.core.PApplet;

public class AnotherClass{
  PApplet parent;

  public AnotherClass( PApplet p) {
      parent = p;

  }
  void update() {
         variable = true; 
         obj.doSomething();
  }
}

How can I, in a not very complicated way, make "AnotherClass" recognise both " obj" and "variable" in a similar manner to how it happens in Processing app? Thanks in advance!

Answers

  • If the classes are in the same package then this should work

    import processing.core.PApplet;
    
    public class AnotherClass{
    
      PApplet parent;
    
      public AnotherClass( PApplet p) {
          parent = p;
      }
    
      void update() {
             parent.variable = true; 
             parent.obj.doSomething();
      }
    }
    
  • edited May 2018

    This solution does make sense to me, but it doesn't work with my setup for some reason. Here is what I get: error

    console:

    java.lang.Error: Unresolved compilation problems: 
        variable cannot be resolved or is not a field
        obj cannot be resolved or is not a field
    
        at AnotherClass.update(AnotherClass.java:12)
        at MainClass.draw(MainClass.java:22)
        at processing.core.PApplet.handleDraw(PApplet.java:2426)
        at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
        at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
    

    Code:

    import processing.core.PApplet;
        public class MainClass extends PApplet {
                MyClass obj;
                AnotherClass obj2;
                boolean variable;
    
            public static void main(String[] args) {
                PApplet.main("MainClass");
    
            }
    
            public void settings() {
                size ( 1280, 720);
            }
    
            public void setup() {
                 obj = new MyClass(this);
                 obj2 = new AnotherClass(this);
            }
    
            public void draw() {
                obj2.update();
            }
        }
    
        import processing.core.PApplet;
    
        public class AnotherClass{
    
          PApplet parent;
    
          public AnotherClass( PApplet p) {
              parent = p;
          }
    
          void update() {
                 parent.variable = true; 
                 parent.obj.doSomething();
          }
        }
    
        import processing.core.PApplet;
    
        public class MyClass {
    
            PApplet parent;
    
            public MyClass(PApplet p) {
                parent = p;
            }
    
            void update() {
    
            }
    
            void doSomething() {
                    System.out.println("yay!");
            }
        }
    
  • How can I, in a not very complicated way, make AnotherClass recognize both obj and variable in a similar manner to how it happens in Processing app?

    • The obj & variable fields are members of class MainClass.
    • For AnotherClass to be able to directly access members of MainClass w/o using the dot . operator: https://Processing.org/reference/dot.html
    • That AnotherClass gotta be nested to class MainClass, so it is a member of it as well.
  • edited May 2018

    @GoToLoop, using the dot operator is fine as long as it works. I am having a problem with it though, please, check my post above.

  • @GotoLoop is right they are members of MainClass not PApplet so my solution is not a solution at all.

  • Correct me if I am wrong, but for a class to be nested in another class, it needs to be in the same class file. It's a bit of a problem for me because I would like to have multiple class files.

  • edited May 2018 Answer ✓
    • Even though MainClass is a subclass of PApplet, fields obj & variable are MainClass's exclusive members.
    • That is, those 2 fields weren't inherited from PApplet. Therefore they're not of datatype PApplet.
    • In order for another class to access those exclusive members, it needs to request a reference of datatype MainClass.
    • Datatype PApplet isn't enough to access them! [-X
    • Unless you cast the PApplet reference w/ (MainClass). :ar!
  • It did work when instead of referencing PApplet I referenced MainClass. Yay! Thank you for your help!

Sign In or Register to comment.