We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
Kf
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?
Just wanted to check is it wrong defining the same thing again in a new class?
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?
Fixed it
Initializing the Main class instead of the PApplet fixed it. Thanks for the help guys.