Aditya Paul
Aditya Paul

Reputation: 61

Coloring a Line in JavaFX and Varying this color

I am new to JavaFX. I was trying to carry out a graphical implementation of an Ant Colony Optimization Algorithm. My version is very simple and involves ants moving in a 2D Cartesian plane, depositing pheromones if and when they find food.

I have written some code in JavaFX which creates a circular node representing the ant and moves it about on the scene of the stage and I think I'll be able to extend this to multiple ants too.

But I want to know how to color the line joining the ant's current position and it's previous position. Basically when the ant finds food and starts returning to the colony, it has to deposit some pheromone along its path and I want this path to be colored as the ant moves along it. Also the pheromone is supposed to evaporate with time. I have an array that stores this value of pheromone strength. So is it further possible to reduce the hue of the color of the line in proportion with the number(pheromone value)?

Thanks.

Upvotes: 0

Views: 499

Answers (1)

AlmasB
AlmasB

Reputation: 3407

You can use Line to add a geometric line segment to your scene graph. Subsequently, you can call setStroke to set the color for that line. The method can take a Color object. There are various methods for manipulating the hue of the color object in the provided link, most notably deriveColor, which can be used to shift the hue value.

Furthermore, once you have added a Line object, you can bind its endX and endY properties to the position of your ant. This way, as the ant moves along some path, it will "drag" the end of the Line object behind it. I believe this is the desired behavior you are attempting to achieve.

Upvotes: 1

Related Questions