java - How to change the color of the rectangle after every cycle count? -


i have rectangle animates left right. want rectangle change color randomly after every cycle count, want change color selected colors only. instance, have color red, blue , orange, want rectangle change color red, blue , orange color only. example, if move left right first blue, after 1 cycle count (when finishes left right) should change color red or orange , on.

here following code:

public class rect extends application {   @override public void start(stage stage) {     pane canvas = new pane();     scene scene = new scene(canvas, 500, 600);     rectangle rect = new rectangle (100, 40, 100, 100);     rect.setarcheight(50);     rect.setarcwidth(50);     rect.setfill(color.blue);      translatetransition tt1 = new translatetransition(duration.millis(3000), rect);     tt1.setbyy(1000f);     tt1.setcyclecount(animation.indefinite);     tt1.setautoreverse(false);     tt1.play();     canvas.getchildren().add(rect);     stage.setscene(scene);     stage.show();  }  public static void main(string[] args) {     launch();     }   } 

use timeline instead of translatetransition. timeline uses collection of keyframes, , keyframe allows specify handler execute when frame reached, property , value property:

import java.util.random;  import javafx.animation.animation; import javafx.animation.keyframe; import javafx.animation.keyvalue; import javafx.animation.timeline; import javafx.application.application; import javafx.scene.scene; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.rectangle; import javafx.stage.stage; import javafx.util.duration;  public class rect extends application {      @override     public void start(stage stage) {         pane canvas = new pane();         scene scene = new scene(canvas, 500, 600);         rectangle rect = new rectangle(100, 40, 100, 100);         rect.setarcheight(50);         rect.setarcwidth(50);         rect.setfill(color.blue);          color[] palette = new color[] { color.red, color.blue, color.orange };         random rng = new random();          timeline tt1 = new timeline(             new keyframe(                 duration.millis(3000),                  e -> rect.setfill(palette[rng.nextint(palette.length)]),                  new keyvalue(rect.yproperty(), 1040)             )         );          tt1.setcyclecount(animation.indefinite);         tt1.setautoreverse(false);         tt1.play();         canvas.getchildren().add(rect);         stage.setscene(scene);         stage.show();      }      public static void main(string[] args) {         launch();     } } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -