Geertjan is a DZone Zone Leader and has posted 453 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started with JOGL on the NetBeans Platform

02.16.2012
| 18933 views |
  • submit to reddit

JOGL is a cool Java 3D library and I was amazed to find I had a working sample on the NetBeans Platform within about 10 minutes of getting started.

  1. Start by clicking the Download button here:

    http://plugins.netbeans.org/plugin/3260/netbeans-opengl-pack

  2. OK. Now you have lots of NBMs. In NetBeans IDE 7.1, go to Tools | Plugins and then, in the Downloaded tab (which is shown below), add all the plugins you downloaded from the site above. However, uncheck the "GLSL editor" module from the list below, since it doesn't install into 7.1 and you will get errors when you try to install it, but don't worry because we won't need it anyway:

    Then click Install above and everything, except the one you excluded, will be installed.

  3. Now go to the Projects window and you'll find lots of samples:


  4. In the list above, choose "GearsDemo.zip" and then click Next. In the next page, shown below, accept all the defaults and click Finish:


  5. Run the application and you should see the following, which is what we're going to be porting to the NetBeans Platform:


  6. Now create a new NetBeans Platform application. In the application, create a module named "JOGLGears", where your functionality will live. There, create a new TopComponent, with whatever name you like. Then add two library wrapper modules (right-click on the Modules node of the application and choose "Add New Library"), one for the gluegen-rt.jar and the other for the jogl.jar. You will find these JARs in the NetBeans user directory (not the installation directory), e.g., ".netbeans" in Linux systems. Then set the dependencies between them as shown below, i.e., importantly, jogl should depend on gluegen-rt and JOGLGears should depend on jogl:


  7. Now switch to the Files window and put the native libraries into a folder named "release/modules/lib", as shown below, which you can create in any of the modules, since they'll all end up in the same place when the application is compiled:


    Next, add this line to "platform.properties" in the "JOGLGearsApp" (i.e., you will find this file in the "Important Files" node of the main application):

    run.args.extra=-J-Djava.library.path=../JOGLGearsApp/release/modules/lib

    For further advice on the above, see this link, which is where I got the tip above.
  8. Copy the "JOGLGearsDemo.java" file from the sample into your "JOGLGears" module. Then copy the annotations from your TopComponent into the file you copied and let the class extend TopComponent. I.e., the end result should be as follows, for the class declaration and the replacement of the Main method:
    @TopComponent.Description(preferredID = "GearsTopComponent",
    //iconBase="SET/PATH/TO/ICON/HERE", 
    persistenceType = TopComponent.PERSISTENCE_ALWAYS)
    @TopComponent.Registration(mode = "editor", openAtStartup = true)
    @ActionID(category = "Window", id = "org.jogl.gears.GearsTopComponent")
    @ActionReference(path = "Menu/Window" /*
     * , position = 333
     */)
    @TopComponent.OpenActionRegistration(displayName = "#CTL_GearsAction",
    preferredID = "GearsTopComponent")
    @NbBundle.Messages({
        "CTL_GearsAction=Gears",
        "CTL_GearsTopComponent=Gears Window",
        "HINT_GearsTopComponent=This is a Gears window"
    })
    public class JOGLGearsDemo extends TopComponent implements GLEventListener, MouseListener, MouseMotionListener {
    
        public JOGLGearsDemo() {
            setLayout(new BorderLayout());
            GLCanvas canvas = new GLCanvas();
    
            canvas.addGLEventListener(this);
            add(canvas, BorderLayout.CENTER);
            final Animator animator = new Animator(canvas);
            addComponentListener(new ComponentAdapter() {
    
                public void windowClosing(WindowEvent e) {
                    // Run this on another thread than the AWT event queue to
                    // make sure the call to Animator.stop() completes before
                    // exiting
                    new Thread(new Runnable() {
    
                        public void run() {
                            animator.stop();
                            System.exit(0);
                        }
                    }).start();
                }
            });
            animator.start();
        }
    
  9. Run the application and you should see the following, which is not static but spinning gears, this time in the NetBeans Platform:

Drag with your mouse in the TopComponent to change the position and angle of the gears. That's it, you're done. You can now continue working with JOGL. Have fun!

Published at DZone with permission of its author, Geertjan Wielenga.