Jim has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Knowing the State of a JavaFX Script Animation

04.25.2008
| 4353 views |
  • submit to reddit

In 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:

Metronome_stopped

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:

Metronome_running

When you click the Pause button, it becomes enabled and the Resume button is disabled:

Metronome_paused

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();
}
}
]
}
}
}

 

170x93_speaker_v4_4 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

Published at DZone with permission of its author, Jim Weaver.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Carl Antaki replied on Fri, 2008/04/25 - 2:43pm

First thanks for these interesting JavaFX tutorials. Do you really think developers will write code using this syntax? I personally find it hard to get used to or will they use the tool that Sun is developing?

Also does your book cover the compiled JavaFX script?

Thanks,


Carl

Jim Weaver replied on Fri, 2008/04/25 - 5:10pm

Once you get used to it, it is very easy and intuitive.  My book covers the interpreted version, but I plan on writing a compiled JavaFX Script book.

 

Thanks,

Jim Weaver

Carl Antaki replied on Fri, 2008/04/25 - 6:30pm in response to: Jim Weaver

I find that the tools available today don't help. I've downloaded Netbeans RC 6.1 and to be able to develop compiled JavaFX script one has to manually download a plugin and the plugin itself doesn't offer any command completion. I know that JavaFX script is not done yet but I haven't seen anything really cool that couldn't be done using Flash or Adobe Air. The cool thing there is the binding which is nice but the animation part at least on the compiled version has no real documentation available.

Jim Weaver replied on Fri, 2008/04/25 - 7:05pm

I think that we'll all get a good feel for JavaFX tool direction at JavaOne -- less than two weeks away!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.