“Star Dust” demo for Trident

October 21st, 2009

With the release 1.1 of Trident animation library available a couple of weeks ago, it’s time to start the development of version 1.2 (code-named Cookie Jar). The first 1.2dev drop has a few minor API enhancements, as well as a small new demo. This demo is based on the Flash mouse star trailer, and is slightly reminiscent of the Glitzer applet from Paul Schmidinger. Here is a video showing “Star Dust” in action:

The code is quite straightforward (see the test.swing.StarDust class). It has a looping timeline that spawns new stars. At every pulse this timeline checks the current location of the mouse. If the mouse is inside the panel, it creates a new Star object, as well as a matching Timeline object. The paintComponent() method of the panel iterates over all “live” stars and paints them based on their current position, size, rotation, alpha and color.

Here is the relevant code for the spawner timeline:

Timeline spawner = new Timeline();
spawner.addCallback(new UIThreadTimelineCallbackAdapter() {
   private float currHue = 0.0f;

   @Override
   public void onTimelinePulse(float durationFraction,
         float timelinePosition) {
      Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
      SwingUtilities.convertPointFromScreen(mouseLoc, mainPanel);
      double currX = mouseLoc.getX();
      double currY = mouseLoc.getY();
      if ((currX < 0) || (currY < 0) || (currX > mainPanel.getWidth())
            || (currY > mainPanel.getHeight()))
         return;

      double outerStartSpan = 5;
      double outerFinalSpan = 20;
      Star star = new Star(currX, currY, outerStartSpan);

Here, we use the MouseInfo class to get the mouse location, and then create a Star object centered at that point. Then, we create a timeline to animate the star location, size, rotation and alpha:

Timeline starTimeline = new Timeline(star);
double angle = Math.random() * 2.0 * Math.PI;
double distance = 20.0 + 30.0 * Math.random();
starTimeline.addPropertyToInterpolate("x", currX, currX
	+ distance * Math.cos(angle));
starTimeline.addPropertyToInterpolate("y", currY, currY
	+ distance * Math.sin(angle));
starTimeline.addPropertyToInterpolate("alpha", 1.0f, 0.0f);
starTimeline.addPropertyToInterpolate("rotation", 0.0f,
	(float) (2 * Math.PI * Math.random()));
starTimeline.addPropertyToInterpolate("outerSpan",
	outerStartSpan, outerFinalSpan);
starTimeline.addPropertyToInterpolate("color", Color.white,
	new Color(Color.HSBtoRGB(currHue, 0.8f, 0.7f)));
currHue += 0.01f;

When the timeline for the specific star is created, it is simply played for 3 seconds. The main spawning timeline is looped indefinitely:

      starTimeline.setDuration(3000);
      starTimeline.play();
   }
});
spawner.playLoop(RepeatBehavior.LOOP);

If you have Java 6 or later on your machine, click on the button below to launch the WebStart demo of “Star Dust”: