Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 16 - Pages: 2   [ 1 2 | Next ]
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Brand New Scene Graph API: Trying It Out

URL: Project Scene Graph

At 1:13 PM on Dec 12, 2007, Geertjan wrote:

Yesterday Josh Marinacci announced that a library called Scene Graph has been spun out of the JavaFX project and open sourced on java.net. According to the site, the library "provides 'scene graph' functionality at the Java level, as well as providing one of the important runtime elements that the JavaFX Script language depends upon from the underlying platform. This project is released in very early access form, so that people can see what we're doing and play with it as we continue developing."

For the full details on the terminology involved in this library, such as "what is a scene graph", and so on, see Josh's blog entry on this subject . The seamless integration with Java of work done for JavaFX is great news. An absolutely wonderful part of his blog entry is where he says: "The API is useful not only from JavaFX and Java applications, but also from Groovy, Python, Ruby, and any other JVM based language." Of course, personally, as someone interested in Groovy, I'm very keen to try this out there. The fact that Josh contrasts this particular API with others in terms of speed indicates that this one is (unlike the others) fast , in addition to being open source and cool. The fact that it is built by the same guys that are behind Java2D tells me that this API must have benefited from lessons that they learnt while working on Java2D, which can only be a very good thing indeed.

David Herron's blog also weighs in with sounds of acclamation. He writes: "Even someone like me who barely knows how to draw graphics can see my way to using this." I highly recommend that anyone interested in this API take a look at Chet Haase's presentation on this subject delivered at Javapolis. Click here to get it; note that it is in PDF format. The presentation is really good in giving a high level overview, as well as some juicy code morsels at the end.

However, all this is talk, even the references to code morsels. In fact, they should make you want to see more, and in context, in the context of a complete little application. So let's put it all together into a small scenario. We will create a JFrame , add a scene, and then add new nodes to the scene. A variety of different types of nodes can be added, from text to shapes to actual Swing components (wrapped in nodes). In this case, we will add a background, a rectangle, and a circle:

However, when the circle is clicked twice, it will blink from blue to red (which you can't see here, obviously) and it will also move:

Here is all the code for the above, with comments interspersed as explanations:

public class SceneGraphDemo {
 
    public static void main(String[] args) {
 
        JFrame f = new JFrame("Moving & Blinking Blue Circle Demo");
 
        //Special panel for holding our scene:
        JSGPanel panel = new JSGPanel();
 
        //Our scene, which will group our nodes:
        SGGroup scene = new SGGroup();
 
        //The first node, which will make the background orange:
        SGShape back = new SGShape();
        back.setShape(new Rectangle(0, 0, 500, 500));
        back.setFillPaint(Color.ORANGE);
        
        //Add the background to the scene:
        scene.add(back);
 
        //The next node, which will be a rectangle:
        SGShape rect = new SGShape();
        rect.setShape(new Rectangle(20, 20, 100, 100));
        rect.setFillPaint(Color.RED);
        rect.setDrawPaint(Color.RED.darker().darker());
        rect.setDrawStroke(new BasicStroke(3));
        rect.setMode(SGAbstractShape.Mode.STROKE_FILL);
        
        //Add the rectangle to the scene:
        scene.add(rect);
 
        //The next node, which will be a circle:
        final SGShape circ = new SGShape();
        circ.setShape(new Ellipse2D.Double(100, 100, 50, 50));
        circ.setFillPaint(Color.BLUE);
        circ.setAntialiasingHint(RenderingHints.VALUE_ANTIALIAS_ON);
 
        //Prepare the circle for transformation:
        final SGTransform.Translate trans = SGTransform.createTranslation(0, 0, circ);
 
        //Add the transformed circle to the scene:
        scene.add(trans);
 
        //Add a mouse adapter, provided by the library:
        trans.addMouseListener(new SGMouseAdapter() {
 
            //Override the mouse clicked event:
            @Override
            public void mouseClicked(MouseEvent evt, SGNode arg1) {
                //Make it blink:
                Clip clip = Clip.create(1000, Clip.INDEFINITE, circ, "fillPaint", Color.RED, Color.BLUE);
                //Make it move:
                Clip clip2 = Clip.create(1000, 1, trans, "translateX", trans.getTranslateX(), 
                  trans.getTranslateX() + 100);
                //...but only if user clicks heels twice to make it so:
                if (evt.getClickCount() == 2) {
                    clip.start();
                    clip2.start();
                }
            }
        });
 
        //Set the panel's scene, which contains all our nodes:
        panel.setScene(scene);
 
        //Add the panel to the frame:
        f.add(panel);
 
        //Show the frame:
        f.pack();
        f.setSize(400, 200);
        f.setVisible(true);
 
    }
}

To run the above, all you need is the JAR from the java.net site. Then run the class above and you'll have the result described. And how would one add Swing components? By wrapping them in the library's SGComponent :

JButton jbutton = new JButton("a button");
SGComponent button = new SGComponent();
button.setComponent(jbutton);
scene.add(button);

When asked how this new library relates to the NetBeans Visual Library, Josh says: "While they are similar in implementation I think they serve different purposes. The Visual Library in the NetBeans Platform was designed for specific types of applications, like the UML modeler. It works at a higher level than a more general purpose Scene Graph. However, in the future I'd like to see the Visual Library sit on top of the Scene Graph to take advantage of the hardware acceleration hooks that will be added in the future."

To get started with the Scene Graph Library, go here on java.net and simply download the JAR. Javadoc is also available, as well as mailing lists where you can ask your questions. Looking forward to seeing how this library develops! I'm also looking forward to (many more) samples.

1 . At 3:10 PM on Dec 12, 2007, Andres Almiray wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Thanks for the link to Chet's slides. Its good to know that the API is not yet finished, that will give me some time to play with it to later fit it into GraphicsBuilder.
2 . At 10:18 PM on Dec 12, 2007, Ahmet A. Akin DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

i wonder how would it be if they use a form of fluent api here. like for the code in your example:

        SGShape rect = new SGShape();
        rect.setShape(new Rectangle(20, 20, 100, 100));
        rect.setFillPaint(Color.RED);
        rect.setDrawPaint(Color.RED.darker().darker());
        rect.setDrawStroke(new BasicStroke(3));
        rect.setMode(SGAbstractShape.Mode.STROKE_FILL);
        scene.add(rect);


could be without the rect declaration, using the chained api:

scene.add(new ShapeBuilder()
        .shape(new Rectangle(20, 20, 100, 100))
        .fillPaint(Color.RED)
        .drawPaint(Color.RED.darker().darker())
        .drawStroke(new BasicStroke(3))
        .mode(SGAbstractShape.Mode.STROKE_FILL).build());


and with static imports

scene.add(new ShapeBuilder()
        .shape(new Rectangle(20, 20, 100, 100))
        .fillPaint(RED)
        .drawPaint(RED.darker().darker())
        .drawStroke(new BasicStroke(3))
        .mode(Mode.STROKE_FILL).build());
3 . At 10:35 PM on Dec 12, 2007, Andres Almiray wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Fluent interface support would be great, but even if it doesn't come out Groovy already offers an option: GraphicsBuilder

http://groovy.codehaus.org/GraphicsBuilder
http://www.jroller.com/aalmiray/entry/first_look_at_graphicspad
4 . At 10:43 PM on Dec 12, 2007, Ahmet A. Akin DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Yes i am aware of the Groovy's GraphicsBuilder, it is pretty good.
But in Java also, a little careful design and some Java 5 goodness can make the code much cleaner as well. in this case setters are ugly, declaration of rect was necessary. i would even make the a method like scene.add(SGShape... shapes)
so all the shapes can be added in the same method call.
of course, i do not know the details of the API, maybe things i wrote are not that aplicable
5 . At 10:51 PM on Dec 12, 2007, Andres Almiray wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

I agree, setters may get in the way, fluent interfaces make it easier for a developer to write the code, alas setters/getters are supposed to be tool friendly, NetBeans seems to tailor some of Sun's latest APIs (JSR295/296) to its needs.
6 . At 5:54 AM on Dec 13, 2007, Fabrizio Giudici wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Indeed the Visual Library is much more than drawing diagrams, as it can be used for more creative tasks , so we really need an integration with SceneGraph. Indeed there are many things that should be better integrated with NB RCP and I think it must be one of the most important targets for 2008.
Fabrizio Giudici, TidalWave - We make Java work. Everywhere.
weblogs.java.net/blog/fabriziogiudici, www.tidalwave.it/blog
Member of the NetBeans Dream Team.
7 . At 5:10 PM on Dec 13, 2007, Andrew McVeigh DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

does anyone know the benefits of this over Jazz, the previous scenegraph technology? Everything I've seen in the demos can easily be done using Jazz.

Is it performance? What are the numbers? Is it easier to program with?

Andrew
8 . At 5:14 PM on Dec 13, 2007, Mikael Grev DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

FYI, Jazz is superseded by Piccolo.
Mikael Grev (grev at miginfocom dot com)
MiG Java Calendar Component, MiG Layout for Swing/SWT (Vote -> JDK)
9 . At 5:37 PM on Dec 13, 2007, Jonny Wray wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Also, last time I checked, Netbeans had a scene graph library you could use standalone. Do these projects, both from Sun, have any relationship?

Jonny
www.jonnywray.com
10 . At 5:28 AM on Dec 14, 2007, Andrew McVeigh DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

yeah, thanks for the info, must have missed it ;-)

actually, piccolo is reasonably cool, but quite anaemic compared to Jazz. In particular, piccolo doesn't have the extensive swing handling of jazz. i.e. embedding widgets into a scenegraph nicely. oh, i think they have some support for it, but as it is intended to be cross platform, it is nowhere near as nice.

so, back to my original question. does anyone know what the new scenegraph api does better than jazz?

Andrew
11 . At 8:28 AM on Dec 14, 2007, Andrew McVeigh DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

I guess one of my other concerns is that this is yet another case of NIH. I've used Jazz (and Piccolo) for over 5 years now (and have no affiliation with HCIL). I've been extremely happy with Jazz in terms of functionality and performance. i.e. 10000 nodes on a scenegraph, swing widgets rotating in real time. The HCIL guys at Maryland more or less founded the ZUI area at any rate...

Couldn't Piccolo have been used as a base? Jazz, with its strong swing support? The codebase is small, it has animation, it's quite well designed. And I know those guys could have done with some more funding -- they have a sponsorship page. They are an academic institution, but they still have to have funding...

Andrew
12 . At 5:03 AM on Dec 15, 2007, Patrick Wright wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

> Couldn't Piccolo have been used as a base? Jazz,
> with its strong swing support? The codebase is
> small, it has animation, it's quite well designed.
> And I know those guys could have done with some more
> funding -- they have a sponsorship page. They are
> an academic institution, but they still have to have
> funding...

They've already talked about this, both Piccolo and Jazz are no longer actively developed. They were both the inspiration (from a message by Josh) and we now an active, supported API that is open source and can move forward with newer features. Specific questions about this should probably go to the scenegraph mailing lists, though.

Regards
Patrick
13 . At 12:31 PM on Dec 15, 2007, Andrew McVeigh DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

None of this answers any of my original questions. I still cannot see a single proposed or existing feature in the scenegraph which does something more or differently to Jazz or Piccolo. Can you point me to a forum posting or link with more specifics? I'm a heavy user of these libraries wishing to know whether i should move across or not? Just a few specifics (that withstand scrutiny) would be enough. Surely there is a detailed rationale somewhere?

> They've already talked about this, both Piccolo and
> Jazz are no longer actively developed. They were both

Well, Jazz is no longer developed because Piccolo has replaced it. Piccolo is stable and mature and still being worked on at HCIL (look at the mailing lists), albeit in a reduced capacity. if you dug into the reasons, you'd find that the guys need a bit more funding: http://www.cs.umd.edu/hcil/piccolo/index.shtml
However, the code is clean and still very maintainable. i've personally extended Jazz for my own work, and found it simple.

I'm sure that HCIL would have jumped at the chance for extra funding. Note that I have absolutely no affiliation in any way with HCIL, apart from being a very satisfied user of their excellent libraries. why reinvent something so good?

> the inspiration (from a message by Josh) and we now
> an active, supported API that is open source and can
> move forward with newer features. Specific questions

this is meaningless doublespeak. Jazz and Piccolo have always been open source, and the APIs are hence also open source. Nothing in Jazz and Piccolo prevent people from moving forward with them by adding newer features. And it would benefit the people (including myself) who have built projects (some very sizeable commercial stuff) on top of these already.

> about this should probably go to the scenegraph
> mailing lists, though.

i'll have a look, and post there. i'm just looking for the actual reasons, without people telling me rubbish. if you don't know the reasons, or the reasons are "we just wanted to make our own, it looked like fun" or "we don't want to use someone else's because don't like the code" then say so. who cares, it's your time and sun's money... but don't bother with reasons which don't patently hold up under scrutiny.

i haven't looked into the sun scenegraph stuff beyond a cursory look at the javadoc, but i wonder if it will support the powerful ZUI scalable zooming (truncation of the graph under a certain visibility) and fisheye views. if not, we've moved backwards and need to copy from Jazz and Piccolo to get back to a level field.

Andrew


> Regards
> Patrick
14 . At 12:54 PM on Dec 15, 2007, Patrick Wright wrote:
  Click to reply to this thread Reply

Re: Brand New Scene Graph API: Trying It Out

Andrew,

I'm not intending to start a fight or continue one. I don't work for Sun and have no relationship to the scenegraph project. According to the Piccolo website, (http://www.cs.umd.edu/hcil/piccolo/learn/about.shtml) Piccolo was a replacement for Jazz, and the Java version of Piccolo is no longer being maintained.

You have some valid points and I suggest you take them up with the scenegraph group. I have no idea why they didn't just take up the Piccolo library. I myself am happy that we may see better integration with the new(er) Java2D pipelines, possible acceleration for some effects via shaders, 3D support in the scenegraph, and a library that works as well on mobile and desktop platforms. So I, at least, am excited.

Best regards,
Patrick

thread.rss_message