June 21st, 2009

Trident part 1 – Hello world

Over the course of the next few days i’m going to talk about different concepts in the Trident animation library for Java applications. Part one introduces the “Hello world” example.

Timeline is the most important concept in Trident. Here is a simple example that illustrates the basic timeline terms:

import org.pushingpixels.trident.Timeline;
 
public class HelloWorld {
	private float value;
 
	public void setValue(float newValue) {
		System.out.println(this.value + " -- " + newValue);
		this.value = newValue;
	}
 
	public static void main(String[] args) {
		HelloWorld hello = new HelloWorld();
		Timeline timeline = new Timeline(hello);
		timeline.addPropertyToInterpolate("value", 0.0f, 1.0f);
		timeline.play();
 
		try {
			Thread.sleep(3000);
		} catch (Exception exc) {
		}
	}
}
  • We import the org.pushingpixels.trident.Timeline class.
  • We define a private float attribute of this class, and provide a public setter for this attribute.
  • We create a new instance of our test class and a timeline associated with that instance.
  • We specify that the value attribute of that instance should be interpolated from 0.of to 1.0f when the timeline is played.
  • Finally, we play the timeline.
  • The Thread.sleep(3000) makes sure that the application waits long enough for the timeline to finish playing

Here is the output of this application:

0.0 -- 0.0
0.0 -- 0.436
0.436 -- 0.514
0.514 -- 0.594
0.594 -- 0.672
0.672 -- 0.75
0.75 -- 0.828
0.828 -- 0.906
0.906 -- 0.984
0.984 -- 1.0
1.0 -- 1.0

Note that the actual numbers depend on the current system load. The setter is called by the timeline at timeline pulses. A timeline pulse is the point where the timeline “wakes up” and changes the values of all registered fields. The values are changed based on how much time has passed since the timeline has started playing.

The three basic timeline concepts illustrated in this sample are:

  • A timeline is associated with an object.
  • A timeline interpolates values of object fields using the public setters of the relevant class.
  • The field values are changed at timeline pulses.