Forums > Java

Instantiating Java Externals

April 7, 2010 | 3:26 am

I’m worried I might have a fundamental misunderstanding of the capacities of Java externals.

I think it boils down to one question. Is it possible to instantiate a Java external from another Java class?

I am trying to create an external called JavaToMax that simply sends messages from Java out to Max. The problem is I want/need those messages to be triggered from the Java side; my Java classes need to be able to send messages to Max whenever they want.

Is this doable? I am under the impression that it is not, since even if you could instantiate and object from Java, there would be no way of identifying WHICH object it is in the patch. Can anyone think of a workaround? If not, I am thinking of switching to OSC.

I’ll include code, not sure how helpful it will be. Ignore the commented stuff and make sure you change the package name.

– Pasted Max Patch, click to expand. –

Thanks!

Attachments:
  1. Main.java

April 7, 2010 | 6:54 am

Java objects hosted within Max can send messages directly into Max. If you are working in another context (i.e. a Java application) then you’ll have to try another route – OSC or just plain UDP will work fine.


April 10, 2010 | 1:05 pm

You can store instances of MaxObjects in a static field, then write static methods that send messages to them.

Writing from memory, it should look something like this.

public class MaxReceiver extends MaxObject {

    static List instances = new ArrayList();

    public MaxReceiver {
        declareOutlets(DataTypes.ALL)
        instances.add(this);
    }

    protected void receive(Atom... message) {
        outlet(0, message);
    }

    public static void send(Atom... message) {
        for (MaxReceiver r : instances) {
            r.receive(message);
        }
    }
}

Above, the send messages sends the given message throught the outlet of all MaxReceiver objects. Alternatively, you could let the users supply a name argument to each MaxReceiver object and provide a public static void send (String name, Atom... message) method.


Viewing 3 posts - 1 through 3 (of 3 total)