0

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.

  • 1
    Why do you need to modify `xx` anyway? – James_D Aug 11 '17 at 11:49
  • @James_D Oh Ive solved this issue already. variable had to be either local or final. So Ive set it as local variable, that would take x from getLayoutX, then it would use relocate(x+=10,y+=10). LayoutX automatically inherits proper value (x position of object) each time i do relocate(). – Peter Anson Aug 11 '17 at 11:54
  • Why not just do `relocate(x+10, y+10)`? Why are you modifying these variables (which you don't seem to use again) in the first place? And "variable had to be either local or final" is not correct. Local variables referenced in a lambda expression must be final. So it cannot be local and non-final. – James_D Aug 11 '17 at 11:55
  • Ohh ok makes sense – Peter Anson Aug 11 '17 at 12:01

1 Answers1

-1

I've solved the issue now.

Code that solved it:

            double x=circle.getLayoutX();
            double y=circle.getLayoutY();

            x+=10;
            y+=10;
            circle.relocate(x,y);