2

I have two types edges in my supply chain model: demand_links and supply_links. the default color is gray for all links. But I want to change the color of demand_links to red each time the attribute of the demand_link is changed (Note:the edge is the custom edge agent through edge creator). How to do this?

Below is my codes for simple test and it didn't work.

    public class EdgeStyle2D extends DefaultStyleOGL2D {

    @Override
    public Color getColor(Object o){

//      if (((CustomEdge) o).getCurrent_dl() == 1) {
//          return Color.RED;       
//      }
//      else {
//          return Color.BLACK;
//      }


        if (o instanceof Distributor) 
            return Color.YELLOW;


        return null;
    }

}

I get the error when initialize.

Caused by: java.lang.ClassCastException: class supplyChainSystem.EdgeStyle2D cannot be cast to class repast.simphony.visualizationOGL2D.EdgeStyleOGL2D (supplyChainSystem.EdgeStyle2D and repast.simphony.visualizationOGL2D.EdgeStyleOGL2D are in unnamed module of loader repast.simphony.plugin.ExtendablePluginClassLoader @61af1510)
Jack
  • 1,339
  • 1
  • 12
  • 31

1 Answers1

3

For styling links in this way, you should follow the example in the zombies.style.LinkStyle class within the Zombies_Demo model. Here's what the relevant parts of that class look like:

public class LinkStyle implements EdgeStyleOGL2D {

    public Color getColor(RepastEdge<?> edge) {
        BaseLink<?> link = (BaseLink<?>) edge;
        return ReLogoSupport.lookupColor(link.getColor());
    }

    public int getLineWidth(RepastEdge<?> edge) {
        return (int) (Math.abs(edge.getWeight()));
    }

}

And you would use a class like this for the network (as opposed to agent) style.

J. Ozik
  • 1,083
  • 5
  • 6
  • why is it implements rather than extends (see example in wolf and sheep)? – Jack Sep 21 '19 at 19:24
  • I just couldn't understand the question mark in "RepastEdge> edge". What is the purpose of it? – Jack Sep 21 '19 at 19:27
  • 1
    @Jack EdgeStyleOGL2D is a Java interface and the example in the predator prey model extends DefaultStyleOGL2D which itself implements StyleOGL2D. Depending on how you want to structure the style you can either create a style class that implements one of the style interfaces, or you can extend one of the default style classes. For links you can extend DefaultEdgeStyleOGL2D. – Eric Tatara Sep 22 '19 at 17:45
  • 1
    @Jack, the > is a Java generic that indicates the class inside the generic <> brackets is undefined. You can omit the > without problems, or you can put your agent class in the brackets like RepastEdge, but this is optional. – Eric Tatara Sep 22 '19 at 17:47
  • thank you for your patient clarification. it's very clear and helpful. – Jack Sep 23 '19 at 01:33
  • one more question. I have edges overlapped with each other (demand & supply links). How can I make the edges with the red color above the edges with black color so I could see the dynamic changes of the red edges? – Jack Sep 23 '19 at 01:51