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

386
August 15, 2017, at 11:36 PM

I have a rectangle which animates from left to right. I want the rectangle to change color randomly after every cycle count, but I want it to change the color from the selected colors only. For instance, I have color red, blue and orange, then I want the rectangle to change the color from those red, blue and orange color only. For example, if it move from left to right then first it is blue, then after one cycle count (when it finishes from left to right) then it should change the color to red or orange and so on.

Here is my 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();
    }
  }
Answer 1

Use a Timeline instead of a TranslateTransition. A Timeline uses a collection of KeyFrames, and a KeyFrame allows you to specify a handler to execute when the frame is reached, as well as a property and value for that 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();
    }
}
Rent Charter Buses Company
READ ALSO
Json string to java object mapping failed with jackson

Json string to java object mapping failed with jackson

I am trying to map my json to POJO class with Jackson but i am receiving null valuesIf i remove @JsonIgnoreProperties annotation then it throws exception of unrecognized property

327
HBase" Failed to start Master : Connection broken for id 1, my id = 0, error = java.io.EOFException

HBase" Failed to start Master : Connection broken for id 1, my id = 0, error = java.io.EOFException

Trying to run HBase in cluster modeI can start region servers but master is not starting

501
Black screen between activities

Black screen between activities

I get a black screen when I start the activity "B" only from the activity "A"; if I start the activity "B" from another activity that isn't "A", this doesn't happenWhy? I try to start an activity "C" with empty layout from the activity "A", but the black screen...

266
JSTL timezone (KST) implementation error found

JSTL timezone (KST) implementation error found

While I practicing JSTL timeZone tag, I tried to run these two actions:

423