animation - Slide in/out JavaFX stage from/to right side of the screen -
i have task make javafx application. when start window should slides smoothly right side of screen , when click on "x" button it's should slides out right side , finishes.
i found possible use simple timeline animation this. made window slide in, when run app, can't figure out how slide out window.
i tried handle defining handler via setoncloserequest() method of stage, stuck 2 problems:
- can't implement animation
- after click on "x" application closed if use consume() method of window event
code:
public class main extends application { public static void main(string[] args) { application.launch(args); } @override public void start(stage stage) { stage.settitle("main"); group root = new group(); scene scene = new scene(root, 300, 250); stage.setscene(scene); rectangle2d primscreenbounds = screen.getprimary().getvisualbounds(); stage.setx(primscreenbounds.getminx() + primscreenbounds.getwidth()); system.out.println(primscreenbounds.getwidth()); stage.sety(primscreenbounds.getminy()); stage.setwidth(0); stage.setheight(primscreenbounds.getheight()); timeline timeline = new timeline(); timeline.setautoreverse(true); writablevalue<double> writablewidth = new writablevalue<double>() { @override public double getvalue() { return stage.getwidth(); } @override public void setvalue(double value) { stage.setwidth(value); } }; keyvalue kv = new keyvalue(writablewidth, 600d); keyframe kf = new keyframe(duration.millis(3000), kv); timeline.getkeyframes().addall(kf); timeline.play(); stage.show(); stage.setoncloserequest(new eventhandler<windowevent>() { @override public void handle(windowevent event) { event.consume(); } }); } }
this works (kind of, not smooth) me:
import javafx.animation.keyframe; import javafx.animation.keyvalue; import javafx.animation.timeline; import javafx.application.application; import javafx.application.platform; import javafx.beans.value.writablevalue; import javafx.event.eventhandler; import javafx.geometry.rectangle2d; import javafx.scene.group; import javafx.scene.scene; import javafx.stage.screen; import javafx.stage.stage; import javafx.stage.windowevent; import javafx.util.duration; public class slidingwindow extends application { public static void main(string[] args) { application.launch(args); } @override public void start(stage stage) { stage.settitle("main"); group root = new group(); scene scene = new scene(root); stage.setscene(scene); rectangle2d primscreenbounds = screen.getprimary().getvisualbounds(); double screenrightedge = primscreenbounds.getmaxx() ; stage.setx(screenrightedge); system.out.println(primscreenbounds.getwidth()); stage.sety(primscreenbounds.getminy()); stage.setwidth(0); stage.setheight(primscreenbounds.getheight()); timeline timeline = new timeline(); writablevalue<double> writablewidth = new writablevalue<double>() { @override public double getvalue() { return stage.getwidth(); } @override public void setvalue(double value) { stage.setx(screenrightedge - value); stage.setwidth(value); } }; keyvalue kv = new keyvalue(writablewidth, 600d); keyframe kf = new keyframe(duration.millis(3000), kv); timeline.getkeyframes().addall(kf); timeline.play(); stage.show(); stage.setoncloserequest(new eventhandler<windowevent>() { @override public void handle(windowevent event) { timeline timeline = new timeline(); keyframe endframe = new keyframe(duration.millis(3000), new keyvalue(writablewidth, 0.0)); timeline.getkeyframes().add(endframe); timeline.setonfinished(e -> platform.runlater(() -> stage.hide())); timeline.play(); event.consume(); } }); } }
the platform.runlater(...)
seems necessary prevent slew of nullpointerexception
s when window hidden, because animation causing system try access stage no longer exists.
Comments
Post a Comment