Search code examples
javamultithreadingprocessingagents-jade

Extracting draw() method on a separate loop (PApplet as a JADE Agent)


I am creating a JADE-based agent system on Java, and I want to use Processing for visualizing these agents. Now the JADE framework runs on it's own, and a Processing PApplet is instantiated as one of the agents, which is a singleton.

Every time one of the (other types of) agents change, they call the redraw() method of the PApplet. The problem is, the PApplet doesn't call it's draw() method, since it's not running on it's own thread.

How do I fix this?

EDIT:

public class Manager extends Agent{
        //The Agent object that runs as a separate thread under JADE framework.
        protected void setup(){
                ...
                javax.swing.SwingUtilities.invokeLater(new VisualizerThreadRunnable(this));
                ...
        }
}
class VisualizerThreadRunnable implements Runnable {
        public VisualizerThreadRunnable(Manager m){
                  ...
        }
        public void run(){
                System.out.println("visualizer being launched...");
                Visualizer visualizer = new Visualizer(manager);
                visualizer.setVisible(true);
        }
}

public class Visualizer extends PApplet {
        //from examples on http://processing.org/tutorials/eclipse/
        public Visualizer(Manager m){
                this.m = m;
                ...
        }
        public void setup() {
                size(200,200);
                background(0);
        }

        public void draw() {
                stroke(255);
                if (mousePressed) {
                        line(mouseX,mouseY,pmouseX,pmouseY);
                }
        }
}

Solution

  • In the visualizer thread you also need to initialize the PApplet using init():

     public void run(){
                    System.out.println("visualizer being launched...");
                    Visualizer visualizer = new Visualizer(manager);
                    visualizer.init();//This is pretty important
                    visualizer.setVisible(true);
            }
    

    For more information checkout PApplet's javadocs.

    This should solve the Processing side of the problem. I've never used Jade before, so I don't know if the thread will stay on. Do check if that happens, if not maybe you should keep that thread running.