I've checked API how to change position of a graphical object. I used setLayoutX and setLayoutY successfully (it continously inkremented object x and y) yet they advised me to use relocate(x,y) instead.
I wanna do proper code but problem is I can't use relocate(x,y) for moving. If I use 30 as parameter it works. But I wanted to use something that would let me move the object. I thought thats the purpose of relocate().
When I set parameters as x++ or x+=10 I get this error "local variables referenced from an inner class must be final or effectively final".
Code:
public class LineClass extends DrawingLine {
private final Group root;
private AnimationTimer timer;
//variable for storing actual frame
private Integer i = 0;
//main timeline
private Timeline timeline;
public LineClass() {
int x = 100;
int y = 200;
Circle circle = new Circle(x,y,10);
root = new Group(circle);
//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
double xx = circle.getLayoutX();
//create a keyValue with factory: scaling the circle 2times
// KeyValue keyValueX = new KeyValue(circle.setLayoutX(3));
KeyValue keyValueX = new KeyValue(circle.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(circle.scaleYProperty(), 2);
//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.millis(1000);
//one can add a specific action when the keyframe is reached
EventHandler onFinished = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
circle.relocate(30,30);
/* double xx = circle.getLayoutX();
circle.setLayoutX(xx+=10);
//reset counter
i = 0;*/
}
};
KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
//timer.start();
}
public Parent getView() {
return root ;
}
}
Thanks in advance.