Java 3d in a NetBeans Platform Application

If you want an excellent introduction into Java 3D, look no further than here. I was following the tutorial I found there to get started with what looked to me as the perfect way of spending a Friday evening, when, somewhere at the end, it told me that:

(…) it does not mix very well with Sun.s swing user interface components. These components are called “lightweight” Lightweight components can be hidden by a Canvas3D even if they are supposed to be at the front.

Obviously, I had been planning on embedding hugely impressive 3D animations into a NetBeans Platform Application, and I was not going to be disappointed, so I fired up NetBeans and did it anyway. Here’s how.

Preconditions: The j3dcore JAR files are downloaded and unpacked. You can find them here. I never trust generic Linux installers, because I have no idea what distro those people had in mind when creating them. So I chose one from the zip binaries and ignored the installers. Select the appropriate one for your machine. Unpack the zip file. There’s a second zip file inside. Unpack it too. Put the JAR files and the .so file somewhere where you want to keep them.

There appears to exist a NetBeans plugin for java-3d, but my NetBeans (7.0.1) didn’t find it, and at least one user comment indicates massive issues, so I didn’t bother with it.

  1. Create a new Application: File -> New Project. In the Wizard, under Choose Project, select NetBeans Modules -> NetBeans Platform Application and click Next. Enter the name of the application. I called it Test3D. Click Finish.
  2. The j3dcore JAR files have to be wrapped into a Module. Expand the Test3D project node, right click Modules and select Add New. Use j3dcore as your project name and click Next.  Use as the Module’s code name base.
  3. Right click the j3dcore module node and choose Properties. Under Libraries, click the Wrapped JARs tab and click Add JAR. Navigate to the j3dcore JAR files, select them all (hold shift or ctrl) and click OK.
  4. You now have a library wrapper module, but you lack the shared object file that will allow them to use you graphics adapter. In Test3D, expand the Important Files node. Open the Project Properties file, and add the following line:run.args.extra=-J-Djava.library.path=<the path to the location of your .so file>

    Do not include the name of the file itself. Be sure to add this to the Project Properties file of your main project, Test3D, and not the wrapper module. It has its own Project Properties file.

  5. Add a new module that creates and displays your 3D scene. Right click Modules under Test3D and click Add New. Enter a name. I chose Universe. Enter your code name base and click Finish.
  6. Add a new TopComponent. Right click the Universe node and click New -> Other. In the New File wizard, select Module Development -> Window. Select Editor as Window Placement and select Open on Application Start. Click Next. Enter UniverseTopComponent after Class Name Prefix and click Finish.
  7. Open UniverseTopComponent. In the Design View, add a JPanel that fully covers the TopComponent. Right click it, select Change Variable Name and enter myUniverse. Right click it again, choose Set Layout -> Border Layout.
  8. Switch to the Source Editor, and change the constructor into this:
public UniverseTopComponent() {

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas = new Canvas3D(config);
    myUniverse.add(canvas, BorderLayout.CENTER);

    BranchGroup group = new BranchGroup();

    Sphere sphere = new Sphere(0.2f);

    Color3f lightColor = new Color3f(1.8f, 0.1f, 0.1f);
    BoundingSphere bounds =
    new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    Vector3f lightDirection = new Vector3f(4.0f, -7.0f, -12.0f);
    DirectionalLight light = new DirectionalLight(lightColor, lightDirection);

    SimpleUniverse universe = new SimpleUniverse(canvas);
  1. Right click the editor window and select Fix Imports from the popup menu. Then build and run the project. Your output should look like this:
Screenshot Test3d Example Application
A bare-bones NetBeans Platform Application showing a Java3D-scene

Conclusion: it is possible to embed a Java 3D scene into a NetBeans Platform Application.

Categorised as: cool stuff, howto, java

Comments are closed.