Reference instance of another PApplet sketch in Eclipse

I use Eclipse and have a main function which calls 2 different PApplets:

public static void main(String... args){
    PApplet.main("Backend.LaunchBackend");
    PApplet.main("Interface.LaunchInterface");
}

Inside Backend, I have several classes which reference to LaunchBackend and are constructed via SomeClass(PApplet parent).

Class SomeClass{
    LaunchBackend parent;
    public SomeClass(LaunchBackend parent){
    this.parent = parent;
}

While coding from within the LaunchBackend PApplet, I can obviously instanciate these via:

SomeClass someClass = new SomeClass(this);

But what if I want to instanciate the class not from within the LaunchBackend PApplet, but from within the LaunchInterface PApplet? I still want the instance of the object to point to LaunchBackend as its parent, like this:

Within Interface:

SomeClass someClass = new SomeClass(***reference to the LaunchBackend PApplet instance***);

Answers

  • edited June 2017 Answer ✓

    I assume that Backend and Interface are package names. If they are then in Java package names start with a lowercase letter. Hence my assumption.

    I have not tested this but I think it should work.

    import Backend.*;
    import Interface.*;
    
    public static void main(String[] args) {
        String[] a = {"MAIN"};
    
        LaunchInterface launchInterface = new LaunchInterface();
        LaunchBackend launchBackend = new LaunchBackend();
    
        // These methods need to be created in these 2 classes
        // and should store the references. Do this before launching the PApplets
        launchInterface.setBackend(launchBackend);
        launchBackend.setInterface(launchInterface);
    
        // Launch the PApplets
        PApplet.runSketch( a, launchInterface);
        PApplet.runSketch( a, launchBackend);
    }
    
Sign In or Register to comment.