Search code examples
colorssimulationanylogicagent

AnyLogic: Dynamically change color of Rectangle when agent is in machine


Attached is a snippet of my implementation. The machine is a resource and modeled as an agent. When clicking on the rectangle, I am redirected to the agent (the resource machine). What happens in this machine is irrelevant to my problem. How can I change the color of the rectangle dynamically?

  1. Simple: if there is an agent in the machine, I want it to show the state yellow. If it is not in, the rectangle should show green.
  2. More complex: If there are few agents in the machine, I want the rectangle to be green and the more agents there are at the current time, the redder the rectangle should turn. So specify a color gradient, which changes according to the given number of agents in the machine.

Can someone help me here? Thanks in advance.

enter image description here


Solution

  • Use the dynamic properties of the rectangle shape for its fill color.

    1. Either check via code or use a boolean flag in your machine isOccupied. Set it to true if someone enters, and false upon exit. Then, you can change the color as below: enter image description here

    2. A bit more complex, you need to define your own function. Switch the boolean variable from above to an integer numVisitors. When someone enters, add to it, when someone leaves, you reduce it by 1. Create the function getMyColor(int numVisitors)returns a Color and take as an input the numVisitors. You need to code the actual gradient yourself, there surely is some Java code online to help with it. Last, let your rectangle color be defined by the function as below: enter image description here