« Reading 'tween the Lines - Simplified JavaFX Script Animation Syntax | Main | Game Over: Improving upon the Compiled JavaFX Tetris Program »

April 25, 2008

Knowing the State of a JavaFX Script Animation

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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54f133d69883400e551fac5728833

Listed below are links to weblogs that reference Knowing the State of a JavaFX Script Animation:

Comments

Mario,

The paused and running attributes have been recently added to the Timeline class, so the plug-in that you're using must not have the most recent JavaFX runtime classes. I would try updating the plug-in.

Hi,

I just get an exception when I try to start this sample from Netbeans 6.1. The symbols running and paused are unknown. But it works when I compile it with the JavaFX compiler and launch it from the commandline.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

My Photo

Upcoming Speaking Engagements:


  • Stephen Chin and Jim Weaver speaking about JavaFX Platform

  • Speaking on JavaFX and Java at Øredev in Malmö, Sweden on 2-6 November, 2009

Upcoming JavaFX Training:


  • Developing Secure, Rich Internet Applications Hosted on a Variety of Clients Using JavaFX Technology

Enter your email address:

Delivered by FeedBurner

Available now as early access eBook


  • Click book image above to obtain eBook

Twitter Updates

    follow me on Twitter

    Affiliations:

    DZone Links:


    July 2009

    Sun Mon Tue Wed Thu Fri Sat
          1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31  

    Disclaimer:

    • By reading this site, you are agreeing that under no circumstances will Veriana Networks, Inc. or its affiliates be responsible for (1) any information contained on or omitted from the site, (2) any person's reliance on any such information, whether or not the information is correct, current or complete, (3) the consequences of any action you or any other person takes or fails to take, whether or not based on information provided by or as a result of the use of the sites.