name123
name123

Reputation: 65

AnyLogic: is it possible to embed code to define a color?

In the Java source code of my class, I saw that color is integrated as a method call with the input color as a word. So it seems that you can't implement any code, because color is defined with shape.setFillColor(). Is there a way around this? Otherwise a more complicated coloring would not be possible. I have attached an example here in this screenshot how I could imagine it, however I get some errors because semicolons are missing and I have not declared the variables. So my problem is that i don't know how and if i can query if-else as well as embed switch case. So how can I write various functions for a color setting? Can someone help me? Example code for a color change of a rectangle

enter image description here

Upvotes: 0

Views: 208

Answers (2)

mczandrea
mczandrea

Reputation: 630

You could define a function that returns Color type, e.g. chooseColor(<your parameters>) and you could call shape.setFillColor(chooseColor(<your parameters>)). The body of the chooseColor() function can be based on the example code you posted.

Or you could call the chooseColor() function directly at the Fill color property of your shape if it is not too computation heavy, otherwise it could slow down the simulation.

Upvotes: 2

Benjamin
Benjamin

Reputation: 1

Not quite sure what you need, tbh. You can make coloring as complex as you like. But once you decide on a color, you must call shape.setFillColor(theColorYouChose) to actually do it...

You can, however, create any possible color using Color myNewColor = new Color(x,y,z,a) where x, y and z are RGB values (0-255) and the last is the transparency you want (also 0-255)

To use this with switch/if statements, you can create a local Color variable and use it at the end for the shape:

Color myColor = new Color(0,0,0,0);
switch(someThing) {
    case myCase:
        myColor = yellowGreen;
        break;
    case myOtherCase:
        if (someCondition) {
            myColor = new Color(12, 222,45,122);
        } else {
            myColor = blue;
        }
        break;
    }
shape.setFillColor(myColor);

Upvotes: 1

Related Questions