Knowing the State of a JavaFX Script Animation
Join the DZone community and get the full member experience.
Join For Freein the reading 'tween the lines - simplified javafx script animation syntax post, i showed you how to start, stop, pause and resume an animation. in this post i'm going to show you how to read the state of the animation (i.e. whether is it running, and whether it is paused). to demonstrate this, i modified the metronome-like example from the previous post . here's a screenshot of today's example when it first starts up:
notice that only the start button is enabled. as shown in the screenshot below, when you click the start button the animation starts, and the enabled state of some of the buttons change:
when you click the pause button, it becomes enabled and the resume button is disabled:
clicking the stop button causes the animation to stop and for the buttons to have the same states shown in the first screenshot. in the code for this example, notice that the buttons' enabled attributes are bound to the running and paused attributes of the timeline instance:
/*
* metronome.fx
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a compiled javafx script example.
*/
import javafx.ui.*;
import javafx.ui.canvas.*;
import javafx.animation.*;
class metronomemodel {
public attribute x2val = 100;
public attribute anim =
timeline {
autoreverse: true
keyframes: [
keyframe {
time: 0s
values: x2val => 100
},
keyframe {
time: 1s
values: x2val => 300 tween interpolator.linear
}
]
repeatcount: timeline.indefinite
};
}
frame {
var metromodel =
metronomemodel {}
title: "animation example"
width: 400
height: 500
visible: true
content:
borderpanel {
center:
canvas {
content:
line {
x1: 200
y1: 400
x2: bind metromodel.x2val
y2: 100
strokewidth: 5
stroke: color.red
}
}
bottom:
flowpanel {
content: [
button {
text: "start"
enabled: bind not metromodel.anim.running
action:
function():void {
metromodel.anim.start();
}
},
button {
text: "pause"
enabled: bind not metromodel.anim.paused and
metromodel.anim.running
action:
function():void {
metromodel.anim.pause();
}
},
button {
text: "resume"
enabled: bind metromodel.anim.paused
action:
function():void {
metromodel.anim.resume();
}
},
button {
text: "stop"
enabled: bind metromodel.anim.running
action:
function():void {
metromodel.anim.stop();
}
}
]
}
}
}
if you have any question, please post a comment. also, if you'll be at javaone 2008, please attend my
javafx script progamming language tutorial
session and introduce yourself afterward!
regards,
jim weaver
javafx script: dynamic java scripting for rich internet/client-side applications
immediate ebook (pdf) download available at
the book's apress site
Opinions expressed by DZone contributors are their own.
Comments