Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Setobjecttothecenterofthescreeninjavafx

I want to move obj to the center of the screen. App is fullscreen and I tried getting Screen resolution, Scene resolution, but it goes not to the center, it goes to some right-down place and each object has it's own position, BUT I get and set constant screen resolution! It doesn't change but I don't know why it depends on obj position. What can I do to make it right?

Here is a void that gets ImageView 'obj' and has to move 'obj' to the center of the screen. Void can be used for different ImageViews with different positions. 'posx' and 'posy' is initial position(it is not constant).

Timeline timeline = new Timeline();
timeline.getKeyFrames().addAll(
        new KeyFrame(Duration.ZERO,
                new KeyValue(obj.translateXProperty(), posx),
                new KeyValue(obj.translateYProperty(), posy)
        ),
        new KeyFrame(new Duration(400),
                new KeyValue(obj.translateXProperty(), stage.getWidth()/2),
                new KeyValue(obj.translateYProperty(), stage.getHeight()/2),
        )
);
timeline.play();

Comments