

Java - Jak projít v Timeline celé pole?
Dobrý den,
Potřebuji upravit tento kód
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
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 Example extends javafx.application.Application {
int index = 0;
@Override
public void start(Stage primaryStage) {
var rectangle = new Rectangle(200, 200, Color.BLACK);
var list_of_color = new Color[]{Color.PINK, Color.MAGENTA, Color.RED, Color.GREEN, Color.YELLOW, Color.TURQUOISE, Color.BLUE, Color.ORANGE};
rectangle.setOnMouseClicked((t) -> {
var timeline = new Timeline(new KeyFrame(Duration.millis(400), (event) -> {
rectangle.setFill(list_of_color[index]);
index += 1;
}));
timeline.setCycleCount(list_of_color.length);
timeline.play();
});
primaryStage.setTitle("Example!");
primaryStage.setScene(new Scene(new Pane(rectangle)));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Jde o to, že musím vytvořit řídící proměnou, abych mohl v timeline projít postupně všechny prvky pole.
A problémem je, že v lambda výrazu nelze používat local variable (local variable referenced from a lambda expression must be final or effectively final), proto jsem musel použít global variable.
Nevíte, prosím Vás, jak to upravit, abych globální proměnou nemusel použít?
Děkuji