Repainting Objects at Runtime

Often developers need to use several copies of the same object but of different colors. A typical case of this is game counters of different colors or similar cars in the racing game.

Car Images in a Racing Game

Multiple copies of similar objects may substantially increase application size. Besides, if you decide to change the image, you will have to perform updates in several places. A more efficient solution is to store a single copy of the image and change its color at runtime using power of JavaFX effects:

Source Code
for (i in [1..count]) ImageView {
  image: image
  effect: ColorAdjust {
    hue: 1.0 - 2.0 * i / count
  }
}

Find the complete example with more details in Vaibhav Choudhary's blog.

 

English
日本語
한국어
简体中文
русский
Português do Brasil