« SoyLatte – Java 6 on OSX Release 1.0 Coding Quickie – display a message while your GWT app loads »

Java SceneGraph API released

Posted December 13th, 2007 by Nazmul

Comprehensive Real-World BlackBerry Training Courses by developerlife.com

Want to learn from the Masters? We’ve created groundbreaking BlackBerry software (we are finalists in the 2009 BlackBerry Developer Challenge). We have a complete set of instructor-led training courses available for BlackBerry development that will turbo-charge your BlackBerry development efforts. Whether you are a newbie or an experienced developer, we have modules that will fit your needs. Learn more about our courses & schedule.

 

Lots of wonderful things are happening for Java desktop apps. Enter the new SceneGraph API for Java. What is a SceneGraph? Java2D is an immediate mode API, which means that your code is executed when the screen must be refreshed/painted, and your code draws to the screen immediately. SceneGraph in contrast is a retained mode API, which means that you declare what you want to happen and when. But the API figures out the caching, dealing with repaints, clipping, and lots of other low level details that you normally have to deal with – which makes it easier to use!

This initial release is in it’s very early stages, and there are lots of powerful and useful features planned for the future. Check out this presentation for more details. The good news with this API is that it’s not only JavaFX compatible, but you can use it with your Swing apps right now. You can make it easier to write Swing apps that use animations, and transparency, and all kinds of other cool effects, without having to spend inordinate amounts of time writing low level code to make it happen. Using a declarative model make it so much easier! This API is not complete though. Currently there’s support for composites that let you change the transparency, and you can do animations to move components (leveraging the TimingFramework), scale and rotate components. You can also apply composite filters. Currently there’s support for just a few things – Image, Shape, Text, and Component (Swing). In the future, there are plans to support:

  1. Lots more filter effects (drop shadow, blur, spotlight, bloom, etc) that leverage hardware acceleration (pixel shaders on your GPU)
  2. State management wrappers
  3. Video and Audio
  4. HTML
  5. 3D components

Download and demo

Download the API here – http://scenegraph.dev.java.net/. It’s released under GPL v2 without a classpath exception. Here’s a cool demo.

Sample code

This is how simple it is to use this API, you can create a SceneGraph group and add it to the SGPanel class, which is a Swing panel:

   1: JFrame f = new JFrame(“Demo");
   2: panel = new JSGPanel();
   3: SGGroup rootNode = new SGGroup();
   4: panel.setScene(rootNode);
   5: f.add(panel);

Then you can add nodes to the SGPanel’s root node, eg, a simple text node with some attributes:

   1: SGText textNode = new SGText();
   2: textNode.setText(“Blah”);
   3: textNode.setMode(SGText.FILL);
   4: textNode.setPaint(Color.RED);
   5: rootNode.add(textNode);

Once your node is created, you can apply filters to it, and even define animation clips for it, all declaratively! Eg:

   1: // Filter - Make textNode translucent
   2: SGComposite comp =
   3: new SGComposite();
   4: comp.setOpacity(.5f);
   5: comp.setChild(textNode);
   6: rootNode.add(comp);
   7:  
   8: // Animation - Fade comp filter over 1 sec
   9: Clip fader = Clip.create(
  10:   1000,      // duration
  11:   comp,      // animating obj
  12:   “opacity”, // animating prop
  13:   1f,        // “from” value
  14:   .5f);      // “to” value
  15: fader.start();

Once you’ve written the declarative code, you don’t have to do anything to render it, since it already knows how to draw itself!

The power and utility of this approach will become apparent when more SG components are available (3D components, HTML content, video, etc). By the way, this API can not only be accessed from Java as well as JavaFX, but also from scripting languages like Groovy, Python, Ruby, and any other language that can run in the Java VM.

 

Comprehensive Real-World BlackBerry Training Courses by developerlife.com

Want to learn from the Masters? We’ve created groundbreaking BlackBerry software (we are finalists in the 2009 BlackBerry Developer Challenge). We have a complete set of instructor-led training courses available for BlackBerry development that will turbo-charge your BlackBerry development efforts. Whether you are a newbie or an experienced developer, we have modules that will fit your needs. Learn more about our courses & schedule.

3 Responses to “Java SceneGraph API released”

  1. Ivan Says:

    The animation flickers wildly on my computer, not sure why.

  2. Nazmul Idris Says:

    Hi Ivan

    What version of Java are you using, and on what OS are you running the VM? Which demo JNLP program did you notice the flickering on? There’s a new JVM (Java 6 Update N) in Beta that uses DirectX 9 to render all the Java2D API calls, you can download it here. Update N should help with rendering issues, if you are using Windows. The current Java6 VM does not use DirectX for optimizing all of the Java2D API (if you use translucency for example, hardware acceleration is not used).

    Nazmul

  3. cq Says:

    There are pre-exisiting VERY mature 3D scenegraphs for Java.

    Try JMonkeyEngine or its successor Ardor3D, for instance.

Leave a Reply

We use Thank Me Later.