Quantcast

Dynamically changing agent representation on display

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dynamically changing agent representation on display

foxtrotalpha
Hi everyone, 

Apologies for the super basic question but I can't seem to figure this out. 

First some context: I've built a game of life model as a Repast learning exercise and have managed to get it running pretty well. However the model is quite kludgy in my opinion, as I have had to make two agent classes (one for live and one for dead cells) with largely repetitive code. 

To display, I have set the display settings to show the dead agent instances as white and the live ones as blue, which is not exactly an elegant solution. 

I want to simplify my code by using only one agent class and giving it a state variable to determine whether it is alive or dead. 

How can I make it so that the agents display differently (in terms of color in this case) based on that state variable? 

Thanks, 
FA

------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically changing agent representation on display

Jonas Andersen
Hi Faaiz,

You can provide a custom implementation of the StyleOGL2D interface. The easiest is probably to extend the class DefaultStyleOGL2D. See below for an example named my.example.MyAgentStyle.

In order to use your custom style, in the Repast UI you can double-click on the display (in the Scenario Tree) and then select the "Agent Style" tab. Then select your agent and in the "Style Class" dropdown either select or enter my.example.MyAgentStyle.


package my.example;

import java.awt.Color;
import repast.simphony.gis.styleEditor.SimpleMarkFactory;
import repast.simphony.visualizationOGL2D.DefaultStyleOGL2D;
import saf.v3d.ShapeFactory2D;
import saf.v3d.scene.VSpatial;

public class MyAgentStyle extends DefaultStyleOGL2D {

    private static SimpleMarkFactory markFac = new SimpleMarkFactory();

    @Override
    public void init(ShapeFactory2D factory) {
        super.init(factory);
    }

    @Override
    public VSpatial getVSpatial(Object agent, VSpatial spatial) {
        if (spatial == null) {
            spatial = shapeFactory.createShape(markFac.getMark("X"), true);
        }
        return spatial;
    }

    @Override
    public Color getColor(Object object) {
        MyAgent a;
        if (object instanceof Turbine) {
            a = (MyAgent) object;
        } else {
            return Color.BLACK;
        }
        if (a.isAlive()) {
            return Color.BLUE;
        } else {
            return Color.WHITE;
        }
    }

    @Override
    public float getRotation(Object object) {
        return 0;
    }

    @Override
    public float getScale(Object object) {
        return 10;
    }

}

Best regards,

Jonas Andersen



On 30 April 2016 at 19:26, Faaiz Ajaz <[hidden email]> wrote:
Hi everyone, 

Apologies for the super basic question but I can't seem to figure this out. 

First some context: I've built a game of life model as a Repast learning exercise and have managed to get it running pretty well. However the model is quite kludgy in my opinion, as I have had to make two agent classes (one for live and one for dead cells) with largely repetitive code. 

To display, I have set the display settings to show the dead agent instances as white and the live ones as blue, which is not exactly an elegant solution. 

I want to simplify my code by using only one agent class and giving it a state variable to determine whether it is alive or dead. 

How can I make it so that the agents display differently (in terms of color in this case) based on that state variable? 

Thanks, 
FA

------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest



------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Loading...