How to call an instance of an object in a new class in an IDE?

edited October 2016 in Library Questions

I was wondering how I would be able to call an instance of a class from the main sketch in on of the classes i have created.

For example here how would I call the instance of WETriangleMesh "cave" in the new class meshpts. I have put notes where I am getting the error.

I have tried to run this in both IntelliJ and Eclipse but I can,t seem to figure it out.

Root Sketch

import processing.core.PApplet;
import peasy.*;
import toxi.geom.mesh.*;
import toxi.processing.ToxiclibsSupport;

public class MainApp extends PApplet {

    PeasyCam cam;
    ToxiclibsSupport gfx;
    WETriangleMesh cave;
    Meshpts pts;


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

    public void settings(){
        size(1400, 800,P3D);
        smooth();
    }

    public void setup(){
        cam = new PeasyCam(this,750,750,0,2200);
        cave = (WETriangleMesh) new STLReader().loadBinary(sketchPath("data/"+"cave.stl"), STLReader.WEMESH);
        pts = new Meshpts(this);
        gfx=new ToxiclibsSupport(this);
    }

    public void draw(){
        background(0);


        stroke(255, 0, 0);
        noFill();

        cave.run();

        pushMatrix();
        fill(40, 120);
        noStroke();
        strokeWeight(1);
        stroke(10);
        lights();
        gfx.mesh(cave, false, 10);
        popMatrix();

    }
}

Class Meshpts

import processing.core.PApplet;


public class Meshpts{
    PApplet p;


    public Meshpts(PApplet p){
        this.p = p;
    }

    void run() {
        cave.computeCentroid(); // error is here
    }

}

Answers

  • Scope problem. cave is defined in MainApp but not visible in Meshpts

    Try p.cave.computeCentroid();

  • I had tried that but it doesn't work.

  • Your class does not have a constructor that inits your PApplet member"p". Try the following minor changes:

    import processing.core.PApplet;
    
    public class Meshpts{
        PApplet p;
    
    
        public Meshpts(PApplet _p){   //TWO CHANGES
            p = _p;
        }
    
        void run() {
            p.cave.computeCentroid(); // ONE CHANGE as suggested by koogs
        }
    
    }
    

    Kf

  • edited October 2016

    I tried this. Sadly it didn't work. When I type "p." I get all the autocomplete suggestions for the functions of the processing core library but not anything new I had defined. If I write p.cave.computeCentroid(); I get "cave cannot be resolved or is not a field" in Eclipse and "cannot resolve symbol "cave" " in IntelliJ.

    However defining it again seems to work, though I'm not sure if thats something I should do?

    import processing.core.PApplet;
    
    public class Meshpts{
        PApplet p;
        WETriangleMesh cave;             // Adding this seems to work
    
        public Meshpts(PApplet _p){   //TWO CHANGES
            p = _p;
        }
    
        void run() {
            cave.computeCentroid(); // Seems to work
        }
    
    }
    

    Just wanted to check is it wrong defining the same thing again in a new class?

  • Answer ✓

    Cave might need to be declared public in your main class.

    Alternatively, add a getter for cave in the main and call that from the class to get cave.

  • I tried declaring it public but it made no difference.

    How would I create a getter?

  • edited October 2016

    Fixed it

    import processing.core.PApplet;
    
    
    public class Meshpts{
        MainApp p;
    
    
        public Meshpts(MainApp p){
            this.p = p;
        }
    
        void run() {
            p.cave.computeCentroid(); 
        }
    
    }
    

    Initializing the Main class instead of the PApplet fixed it. Thanks for the help guys.

Sign In or Register to comment.